Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions engine/packages/pegboard-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
use http_body_util::{BodyExt, Full};
use hyper::{Request, Response, StatusCode};
use pegboard::tunnel::id::{self as tunnel_id, RequestId};
use rand::Rng;

Check warning on line 9 in engine/packages/pegboard-gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/lib.rs

Check warning on line 9 in engine/packages/pegboard-gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/lib.rs

Check warning on line 9 in engine/packages/pegboard-gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/lib.rs
use rivet_error::*;
use rivet_guard_core::{
WebSocketHandle,
custom_serve::{CustomServeTrait, HibernationResult},
errors::{ServiceUnavailable, WebSocketServiceUnavailable},
proxy_service::{ResponseBody, is_ws_hibernate},
proxy_service::{is_ws_hibernate, ResponseBody},
request_context::RequestContext,
websocket_handle::WebSocketReceiver,
WebSocketHandle,
};
use rivet_runner_protocol as protocol;
use rivet_util::serde::HashableMap;
use std::{sync::Arc, time::Duration};

Check warning on line 21 in engine/packages/pegboard-gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/lib.rs

Check warning on line 21 in engine/packages/pegboard-gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/lib.rs

Check warning on line 21 in engine/packages/pegboard-gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/lib.rs
use tokio::{
sync::{Mutex, watch},
sync::{watch, Mutex},
task::JoinHandle,
};
use tokio_tungstenite::tungstenite::{
protocol::frame::{coding::CloseCode, CloseFrame},

Check warning on line 27 in engine/packages/pegboard-gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/lib.rs

Check warning on line 27 in engine/packages/pegboard-gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/lib.rs

Check warning on line 27 in engine/packages/pegboard-gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/lib.rs
Message,
protocol::frame::{CloseFrame, coding::CloseCode},
};

use crate::shared_state::{InFlightRequestHandle, SharedState};
Expand Down Expand Up @@ -486,7 +486,7 @@
};

// Send close frame to runner if not hibernating
if lifecycle_res
if !&lifecycle_res
.as_ref()
.map_or_else(is_ws_hibernate, |_| false)
{
Expand Down
7 changes: 5 additions & 2 deletions engine/packages/pegboard-gateway/src/shared_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
entry.msg_tx = msg_tx;
entry.drop_tx = drop_tx;
entry.opened = false;
entry.message_index = 0;

if entry.stopping {
entry.hibernation_state = None;
Expand Down Expand Up @@ -192,8 +191,13 @@
send_instant: Instant::now(),
message_index: current_message_index,
};

Check warning on line 194 in engine/packages/pegboard-gateway/src/shared_state.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/shared_state.rs

Check warning on line 194 in engine/packages/pegboard-gateway/src/shared_state.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/shared_state.rs

Check warning on line 194 in engine/packages/pegboard-gateway/src/shared_state.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/rivet/rivet/engine/packages/pegboard-gateway/src/shared_state.rs
hs.pending_ws_msgs.push(pending_ws_msg);
tracing::debug!(
index=current_message_index,
new_count=hs.pending_ws_msgs.len(),
"pushed pending websocket message"
);
}

self.ups
Expand Down Expand Up @@ -391,7 +395,6 @@

let len_after = hs.pending_ws_msgs.len();
tracing::debug!(
request_id=?tunnel_id::request_id_to_string(&request_id),
ack_index,
removed_count = len_before - len_after,
remaining_count = len_after,
Expand Down
108 changes: 101 additions & 7 deletions engine/sdks/typescript/runner/src/actor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type * as protocol from "@rivetkit/engine-runner-protocol";

Check failure on line 1 in engine/sdks/typescript/runner/src/actor.ts

View workflow job for this annotation

GitHub Actions / quality

assist/source/organizeImports

The imports and exports are not sorted.

Check failure on line 1 in engine/sdks/typescript/runner/src/actor.ts

View workflow job for this annotation

GitHub Actions / quality

assist/source/organizeImports

The imports and exports are not sorted.

Check failure on line 1 in engine/sdks/typescript/runner/src/actor.ts

View workflow job for this annotation

GitHub Actions / quality

assist/source/organizeImports

The imports and exports are not sorted.

Check failure on line 1 in engine/sdks/typescript/runner/src/actor.ts

View workflow job for this annotation

GitHub Actions / quality

assist/source/organizeImports

The imports and exports are not sorted.

Check failure on line 1 in engine/sdks/typescript/runner/src/actor.ts

View workflow job for this annotation

GitHub Actions / quality

assist/source/organizeImports

The imports and exports are not sorted.
import type { PendingRequest } from "./tunnel";
import type { WebSocketTunnelAdapter } from "./websocket-tunnel-adapter";
import { arraysEqual } from "./utils";
import { arraysEqual, promiseWithResolvers } from "./utils";
import { logger } from "./log";
import * as tunnelId from "./tunnel-id";

export interface ActorConfig {
name: string;
Expand All @@ -24,11 +26,28 @@
requestId: protocol.RequestId;
ws: WebSocketTunnelAdapter;
}> = [];
actorStartPromise: ReturnType<typeof promiseWithResolvers<void>>;

constructor(actorId: string, generation: number, config: ActorConfig) {
/**
* If restoreHibernatingRequests has been called. This is used to assert
* that the caller is implemented correctly.
**/
hibernationRestored: boolean = false;

constructor(
actorId: string,
generation: number,
config: ActorConfig,
/**
* List of hibernating requests provided by the gateway on actor start.
* This represents the WebSocket connections that the gateway knows about.
**/
public hibernatingRequests: readonly protocol.HibernatingRequest[],
) {
this.actorId = actorId;
this.generation = generation;
this.config = config;
this.actorStartPromise = promiseWithResolvers();
}

// Pending request methods
Expand All @@ -43,13 +62,78 @@
)?.request;
}

setPendingRequest(
createPendingRequest(
gatewayId: protocol.GatewayId,
requestId: protocol.RequestId,
clientMessageIndex: number,
) {
const exists =
this.getPendingRequest(gatewayId, requestId) !== undefined;
if (exists) {
logger()?.warn({
msg: "attempting to set pending request twice, replacing existing",
gatewayId: tunnelId.gatewayIdToString(gatewayId),
requestId: tunnelId.requestIdToString(requestId),
});
// Delete existing pending request before adding the new one
this.deletePendingRequest(gatewayId, requestId);
}
this.pendingRequests.push({
gatewayId,
requestId,
request: {
resolve: () => {},
reject: () => {},
actorId: this.actorId,
gatewayId: gatewayId,
requestId: requestId,
clientMessageIndex,
},
});
logger()?.debug({
msg: "added pending request",
gatewayId: tunnelId.gatewayIdToString(gatewayId),
requestId: tunnelId.requestIdToString(requestId),
length: this.pendingRequests.length,
});
}

createPendingRequestWithStreamController(
gatewayId: protocol.GatewayId,
requestId: protocol.RequestId,
request: PendingRequest,
clientMessageIndex: number,
streamController: ReadableStreamDefaultController<Uint8Array>,
) {
this.deletePendingRequest(gatewayId, requestId);
this.pendingRequests.push({ gatewayId, requestId, request });
const exists =
this.getPendingRequest(gatewayId, requestId) !== undefined;
if (exists) {
logger()?.warn({
msg: "attempting to set pending request twice, replacing existing",
gatewayId: tunnelId.gatewayIdToString(gatewayId),
requestId: tunnelId.requestIdToString(requestId),
});
// Delete existing pending request before adding the new one
this.deletePendingRequest(gatewayId, requestId);
}
this.pendingRequests.push({
gatewayId,
requestId,
request: {
resolve: () => {},
reject: () => {},
actorId: this.actorId,
gatewayId: gatewayId,
requestId: requestId,
clientMessageIndex,
streamController,
},
});
logger()?.debug({
msg: "added pending request with stream controller",
gatewayId: tunnelId.gatewayIdToString(gatewayId),
requestId: tunnelId.requestIdToString(requestId),
length: this.pendingRequests.length,
});
}

deletePendingRequest(
Expand All @@ -63,6 +147,12 @@
);
if (index !== -1) {
this.pendingRequests.splice(index, 1);
logger()?.debug({
msg: "removed pending request",
gatewayId: tunnelId.gatewayIdToString(gatewayId),
requestId: tunnelId.requestIdToString(requestId),
length: this.pendingRequests.length,
});
}
}

Expand All @@ -83,7 +173,11 @@
requestId: protocol.RequestId,
ws: WebSocketTunnelAdapter,
) {
this.deleteWebSocket(gatewayId, requestId);
const exists = this.getWebSocket(gatewayId, requestId) !== undefined;
if (exists) {
logger()?.warn({ msg: "attempting to set websocket twice" });
return;
}
this.webSockets.push({ gatewayId, requestId, ws });
}

Expand Down
Loading
Loading