Skip to content

Commit 426a752

Browse files
committed
chore(rivetkit): implement new hibernating ws protocol
1 parent 769c3f8 commit 426a752

File tree

6 files changed

+547
-826
lines changed

6 files changed

+547
-826
lines changed

engine/sdks/typescript/runner-protocol/src/index.ts

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

engine/sdks/typescript/runner/src/mod.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,40 @@ export interface RunnerConfig {
5757
requestId: protocol.RequestId,
5858
request: Request,
5959
) => Promise<Response>;
60-
websocket?: (
60+
websocket: (
6161
runner: Runner,
6262
actorId: string,
6363
ws: any,
6464
requestId: protocol.RequestId,
6565
request: Request,
66-
) => Promise<void>;
66+
) => void;
6767
onActorStart: (
6868
actorId: string,
6969
generation: number,
7070
config: ActorConfig,
7171
) => Promise<void>;
72+
/**
73+
* Called on actor start.
74+
*
75+
* Returns the message indices for hibernating request IDs.
76+
*
77+
* Disconnect any connections that are not in the request ID list.
78+
*/
79+
restoreHibernativeRequests(
80+
actorId: string,
81+
requestId: protocol.RequestId[],
82+
): number[];
7283
onActorStop: (actorId: string, generation: number) => Promise<void>;
73-
getActorHibernationConfig: (
84+
canWebSocketHibernate: (
7485
actorId: string,
7586
requestId: ArrayBuffer,
7687
request: Request,
77-
) => HibernationConfig;
88+
) => boolean;
7889
noAutoShutdown?: boolean;
7990
}
8091

8192
export interface HibernationConfig {
82-
enabled: boolean;
83-
lastMsgIndex: number | undefined;
93+
lastMsgIndex: number;
8494
}
8595

8696
export interface KvListOptions {
@@ -105,7 +115,6 @@ export class Runner {
105115
}
106116

107117
#actors: Map<string, ActorInstance> = new Map();
108-
#actorWebSockets: Map<string, Set<WebSocketTunnelAdapter>> = new Map();
109118

110119
// WebSocket
111120
#pegboardWebSocket?: WebSocket;
@@ -831,6 +840,11 @@ export class Runner {
831840
webSockets: new Set(),
832841
};
833842

843+
this.#tunnel.restoreHibernatingRequests(
844+
actorId,
845+
startCommand.hibernatingRequestIds,
846+
);
847+
834848
this.#actors.set(actorId, instance);
835849

836850
this.#sendActorStateUpdate(actorId, generation, "running");
@@ -1427,8 +1441,10 @@ export class Runner {
14271441
}
14281442
}
14291443

1430-
sendWebsocketMessageAck(requestId: ArrayBuffer, index: number) {
1431-
this.#tunnel?.__ackWebsocketMessage(requestId, index);
1444+
sendHibernatableWebSocketMessageAck(requestId: ArrayBuffer, index: number) {
1445+
if (!this.#tunnel)
1446+
throw new Error("missing tunnel to send message ack");
1447+
this.#tunnel.sendHibernatableWebSocketMessageAck(requestId, index);
14321448
}
14331449

14341450
getServerlessInitPacket(): string | undefined {

0 commit comments

Comments
 (0)