forked from get-convex/convex-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_socket_manager.ts
537 lines (498 loc) · 17.1 KB
/
web_socket_manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
import { Logger } from "../logging.js";
import {
ClientMessage,
encodeClientMessage,
parseServerMessage,
ServerMessage,
} from "./protocol.js";
const CLOSE_NORMAL = 1000;
const CLOSE_GOING_AWAY = 1001;
const CLOSE_NO_STATUS = 1005;
/** Convex-specific close code representing a "404 Not Found".
* The edge Onramp accepts websocket upgrades before confirming that the
* intended destination exists, so this code is sent once we've discovered that
* the destination does not exist.
*/
const CLOSE_NOT_FOUND = 4040;
/**
* The various states our WebSocket can be in:
*
* - "disconnected": We don't have a WebSocket, but plan to create one.
* - "connecting": We have created the WebSocket and are waiting for the
* `onOpen` callback.
* - "ready": We have an open WebSocket.
* - "stopped": The WebSocket was closed and a new one can be created via `.restart()`.
* - "terminated": We have closed the WebSocket and will never create a new one.
*
*
* WebSocket State Machine
* -----------------------
* initialState: disconnected
* validTransitions:
* disconnected:
* new WebSocket() -> connecting
* terminate() -> terminated
* connecting:
* onopen -> ready
* close() -> disconnected
* terminate() -> terminated
* ready:
* close() -> disconnected
* stop() -> stopped
* terminate() -> terminated
* stopped:
* restart() -> connecting
* terminate() -> terminated
* terminalStates:
* terminated
*
*
*
* ┌────────────────┐
* ┌────terminate()────────│ disconnected │◀─┐
* │ └────────────────┘ │
* ▼ │ ▲ │
* ┌────────────────┐ new WebSocket() │ │
* ┌─▶│ terminated │◀──────┐ │ │ │
* │ └────────────────┘ │ │ │ │
* │ ▲ terminate() │ close() close()
* │ terminate() │ │ │ │
* │ │ │ ▼ │ │
* │ ┌────────────────┐ └───────┌────────────────┐ │
* │ │ stopped │──restart()───▶│ connecting │ │
* │ └────────────────┘ └────────────────┘ │
* │ ▲ │ │
* │ │ onopen │
* │ │ │ │
* │ │ ▼ │
* terminate() │ ┌────────────────┐ │
* │ └────────stop()─────────│ ready │──┘
* │ └────────────────┘
* │ │
* │ │
* └────────────────────────────────────────────┘
*
* The `connecting` and `ready` state have a sub-state-machine for pausing.
*/
type Socket =
| { state: "disconnected" }
| { state: "connecting"; ws: WebSocket; paused: "yes" | "no" }
| { state: "ready"; ws: WebSocket; paused: "yes" | "no" | "uninitialized" }
| { state: "stopped" }
| { state: "terminated" };
export type ReconnectMetadata = {
connectionCount: number;
lastCloseReason: string | null;
};
export type OnMessageResponse = {
hasSyncedPastLastReconnect: boolean;
};
/**
* A wrapper around a websocket that handles errors, reconnection, and message
* parsing.
*/
export class WebSocketManager {
private socket: Socket;
private connectionCount: number;
private lastCloseReason: string | null;
/** Upon HTTPS/WSS failure, the first jittered backoff duration, in ms. */
private readonly initialBackoff: number;
/** We backoff exponentially, but we need to cap that--this is the jittered max. */
private readonly maxBackoff: number;
/** How many times have we failed consecutively? */
private retries: number;
/** How long before lack of server response causes us to initiate a reconnect,
* in ms */
private readonly serverInactivityThreshold: number;
private reconnectDueToServerInactivityTimeout: ReturnType<
typeof setTimeout
> | null;
private readonly uri: string;
private readonly onOpen: (reconnectMetadata: ReconnectMetadata) => void;
private readonly onResume: () => void;
private readonly onMessage: (message: ServerMessage) => OnMessageResponse;
private readonly webSocketConstructor: typeof WebSocket;
private readonly logger: Logger;
constructor(
uri: string,
callbacks: {
onOpen: (reconnectMetadata: ReconnectMetadata) => void;
onResume: () => void;
onMessage: (message: ServerMessage) => OnMessageResponse;
},
webSocketConstructor: typeof WebSocket,
logger: Logger,
) {
this.webSocketConstructor = webSocketConstructor;
this.socket = { state: "disconnected" };
this.connectionCount = 0;
this.lastCloseReason = "InitialConnect";
this.initialBackoff = 100;
this.maxBackoff = 16000;
this.retries = 0;
this.serverInactivityThreshold = 30000;
this.reconnectDueToServerInactivityTimeout = null;
this.uri = uri;
this.onOpen = callbacks.onOpen;
this.onResume = callbacks.onResume;
this.onMessage = callbacks.onMessage;
this.logger = logger;
this.connect();
}
private setSocketState(state: Socket) {
this.socket = state;
this._logVerbose(
`socket state changed: ${this.socket.state}, paused: ${
"paused" in this.socket ? this.socket.paused : undefined
}`,
);
}
private connect() {
if (this.socket.state === "terminated") {
return;
}
if (
this.socket.state !== "disconnected" &&
this.socket.state !== "stopped"
) {
throw new Error(
"Didn't start connection from disconnected state: " + this.socket.state,
);
}
const ws = new this.webSocketConstructor(this.uri);
this._logVerbose("constructed WebSocket");
this.setSocketState({
state: "connecting",
ws,
paused: "no",
});
// Kick off server inactivity timer before WebSocket connection is established
// so we can detect cases where handshake fails.
// The `onopen` event only fires after the connection is established:
// Source: https://datatracker.ietf.org/doc/html/rfc6455#page-19:~:text=_The%20WebSocket%20Connection%20is%20Established_,-and
this.resetServerInactivityTimeout();
ws.onopen = () => {
this.logger.logVerbose("begin ws.onopen");
if (this.socket.state !== "connecting") {
throw new Error("onopen called with socket not in connecting state");
}
this.setSocketState({
state: "ready",
ws,
paused: this.socket.paused === "yes" ? "uninitialized" : "no",
});
this.resetServerInactivityTimeout();
if (this.socket.paused === "no") {
this.onOpen({
connectionCount: this.connectionCount,
lastCloseReason: this.lastCloseReason,
});
}
if (this.lastCloseReason !== "InitialConnect") {
this.logger.log("WebSocket reconnected");
}
this.connectionCount += 1;
this.lastCloseReason = null;
};
// NB: The WebSocket API calls `onclose` even if connection fails, so we can route all error paths through `onclose`.
ws.onerror = (error) => {
const message = (error as ErrorEvent).message;
this.logger.log(`WebSocket error: ${message}`);
};
ws.onmessage = (message) => {
this.resetServerInactivityTimeout();
const serverMessage = parseServerMessage(JSON.parse(message.data));
this._logVerbose(`received ws message with type ${serverMessage.type}`);
const response = this.onMessage(serverMessage);
if (response.hasSyncedPastLastReconnect) {
// Reset backoff to 0 once all outstanding requests are complete.
this.retries = 0;
}
};
ws.onclose = (event) => {
this._logVerbose("begin ws.onclose");
if (this.lastCloseReason === null) {
this.lastCloseReason = event.reason ?? "OnCloseInvoked";
}
if (
event.code !== CLOSE_NORMAL &&
event.code !== CLOSE_GOING_AWAY && // This commonly gets fired on mobile apps when the app is backgrounded
event.code !== CLOSE_NO_STATUS &&
event.code !== CLOSE_NOT_FOUND // Note that we want to retry on a 404, as it can be transient during a push.
) {
let msg = `WebSocket closed with code ${event.code}`;
if (event.reason) {
msg += `: ${event.reason}`;
}
this.logger.log(msg);
}
this.scheduleReconnect();
return;
};
}
/**
* @returns The state of the {@link Socket}.
*/
socketState(): string {
return this.socket.state;
}
/**
* @param message - A ClientMessage to send.
* @returns Whether the message (might have been) sent.
*/
sendMessage(message: ClientMessage) {
const messageForLog = {
type: message.type,
...(message.type === "Authenticate" && message.tokenType === "User"
? {
value: `...${message.value.slice(-7)}`,
}
: {}),
};
if (this.socket.state === "ready" && this.socket.paused === "no") {
const encodedMessage = encodeClientMessage(message);
const request = JSON.stringify(encodedMessage);
try {
this.socket.ws.send(request);
} catch (error: any) {
this.logger.log(
`Failed to send message on WebSocket, reconnecting: ${error}`,
);
this.closeAndReconnect("FailedToSendMessage");
}
// We are not sure if this was sent or not.
this._logVerbose(
`sent message with type ${message.type}: ${JSON.stringify(
messageForLog,
)}`,
);
return true;
}
this._logVerbose(
`message not sent (socket state: ${this.socket.state}, paused: ${"paused" in this.socket ? this.socket.paused : undefined}): ${JSON.stringify(
messageForLog,
)}`,
);
return false;
}
private resetServerInactivityTimeout() {
if (this.socket.state === "terminated") {
// Don't reset any timers if we were trying to terminate.
return;
}
if (this.reconnectDueToServerInactivityTimeout !== null) {
clearTimeout(this.reconnectDueToServerInactivityTimeout);
this.reconnectDueToServerInactivityTimeout = null;
}
this.reconnectDueToServerInactivityTimeout = setTimeout(() => {
this.closeAndReconnect("InactiveServer");
}, this.serverInactivityThreshold);
}
private scheduleReconnect() {
this.socket = { state: "disconnected" };
const backoff = this.nextBackoff();
this.logger.log(`Attempting reconnect in ${backoff}ms`);
setTimeout(() => this.connect(), backoff);
}
/**
* Close the WebSocket and schedule a reconnect.
*
* This should be used when we hit an error and would like to restart the session.
*/
private closeAndReconnect(closeReason: string) {
this._logVerbose(`begin closeAndReconnect with reason ${closeReason}`);
switch (this.socket.state) {
case "disconnected":
case "terminated":
case "stopped":
// Nothing to do if we don't have a WebSocket.
return;
case "connecting":
case "ready": {
this.lastCloseReason = closeReason;
// Close the old socket asynchronously, we'll open a new socket in reconnect.
void this.close();
this.scheduleReconnect();
return;
}
default: {
// Enforce that the switch-case is exhaustive.
const _: never = this.socket;
}
}
}
/**
* Close the WebSocket, being careful to clear the onclose handler to avoid re-entrant
* calls. Use this instead of directly calling `ws.close()`
*
* It is the callers responsibility to update the state after this method is called so that the
* closed socket is not accessible or used again after this method is called
*/
private close(): Promise<void> {
switch (this.socket.state) {
case "disconnected":
case "terminated":
case "stopped":
// Nothing to do if we don't have a WebSocket.
return Promise.resolve();
case "connecting": {
const ws = this.socket.ws;
return new Promise((r) => {
ws.onclose = () => {
this._logVerbose("Closed after connecting");
r();
};
ws.onopen = () => {
this._logVerbose("Opened after connecting");
ws.close();
};
});
}
case "ready": {
this._logVerbose("ws.close called");
const ws = this.socket.ws;
const result: Promise<void> = new Promise((r) => {
ws.onclose = () => {
r();
};
});
ws.close();
return result;
}
default: {
// Enforce that the switch-case is exhaustive.
const _: never = this.socket;
return Promise.resolve();
}
}
}
/**
* Close the WebSocket and do not reconnect.
* @returns A Promise that resolves when the WebSocket `onClose` callback is called.
*/
terminate(): Promise<void> {
if (this.reconnectDueToServerInactivityTimeout) {
clearTimeout(this.reconnectDueToServerInactivityTimeout);
}
switch (this.socket.state) {
case "terminated":
case "stopped":
case "disconnected":
case "connecting":
case "ready": {
const result = this.close();
this.setSocketState({ state: "terminated" });
return result;
}
default: {
// Enforce that the switch-case is exhaustive.
const _: never = this.socket;
throw new Error(
`Invalid websocket state: ${(this.socket as any).state}`,
);
}
}
}
stop(): Promise<void> {
switch (this.socket.state) {
case "terminated":
// If we're terminating we ignore stop
return Promise.resolve();
case "connecting":
case "stopped":
case "disconnected":
case "ready": {
const result = this.close();
this.socket = { state: "stopped" };
return result;
}
default: {
// Enforce that the switch-case is exhaustive.
const _: never = this.socket;
return Promise.resolve();
}
}
}
/**
* Create a new WebSocket after a previous `stop()`, unless `terminate()` was
* called before.
*/
restart(): void {
switch (this.socket.state) {
case "stopped":
break;
case "terminated":
case "connecting":
case "ready":
case "disconnected":
this.logger.warn("Restart called without stopping first");
return;
default: {
// Enforce that the switch-case is exhaustive.
const _: never = this.socket;
}
}
this.connect();
}
pause(): void {
switch (this.socket.state) {
case "disconnected":
case "stopped":
case "terminated":
// If already stopped or stopping ignore.
return;
case "connecting":
case "ready": {
this.socket = { ...this.socket, paused: "yes" };
return;
}
default: {
// Enforce that the switch-case is exhaustive.
const _: never = this.socket;
return;
}
}
}
/**
* Resume the state machine if previously paused.
*/
resume(): void {
switch (this.socket.state) {
case "connecting":
this.socket = { ...this.socket, paused: "no" };
return;
case "ready":
if (this.socket.paused === "uninitialized") {
this.socket = { ...this.socket, paused: "no" };
this.onOpen({
connectionCount: this.connectionCount,
lastCloseReason: this.lastCloseReason,
});
} else if (this.socket.paused === "yes") {
this.socket = { ...this.socket, paused: "no" };
this.onResume();
}
return;
case "terminated":
case "stopped":
case "disconnected":
// Ignore resume if not paused, perhaps we already resumed.
return;
default: {
// Enforce that the switch-case is exhaustive.
const _: never = this.socket;
}
}
this.connect();
}
private _logVerbose(message: string) {
this.logger.logVerbose(message);
}
private nextBackoff(): number {
const baseBackoff = this.initialBackoff * Math.pow(2, this.retries);
this.retries += 1;
const actualBackoff = Math.min(baseBackoff, this.maxBackoff);
const jitter = actualBackoff * (Math.random() - 0.5);
return actualBackoff + jitter;
}
}