Skip to content

attempt close and reconnect for all stopped/disconnected states #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
temp/
dist
node_modules
.vscode

# The convex npm package does not use a package-lock.json.
package-lock.json
Expand Down
73 changes: 46 additions & 27 deletions src/browser/sync/web_socket_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,34 @@ const CLOSE_NOT_FOUND = 4040;
* terminate() -> terminated
* stopped:
* restart() -> connecting
* scheduleReconnect() -> connecting
* terminate() -> terminated
* terminalStates:
* terminated
*
*
*
* ┌────────────────┐
* ┌────terminate()────────│ disconnected │◀─┐
* │ └────────────────┘ │
* ▼ │ ▲ │
* ┌────────────────┐ new WebSocket() │ │
* ┌─▶│ terminated │◀──────┐ │ │ │
* │ └────────────────┘ │ │ │ │
* │ ▲ terminate() │ close() close()
* │ terminate() │ │ │ │
* │ │ │ ▼ │ │
* │ ┌────────────────┐ └───────┌────────────────┐ │
* │ │ stopped │──restart()───▶│ connecting │ │
* │ └────────────────┘ └────────────────┘ │
* │ ▲ │ │
* │ │ onopen │
* │ │ │ │
* │ │ ▼ │
* terminate() │ ┌────────────────┐ │
* │ └────────stop()─────────│ ready │──┘
* ┌───────────────────┐
* ┌────terminate()────────│ disconnected │◀────┐
* │ └───────────────────┘ │
* ▼ │ ▲ ▲ │
* ┌────────────────┐ new WebSocket() │ │ │
* ┌─▶│ terminated │◀──────┐ │ │ │ │
* │ └────────────────┘ │ │ │ │ │
* │ ▲ terminate() │ close() │ close()
* │ terminate() │ │ │ │ │
* │ │ │ ▼ │ │ │
* │ ┌────────────────┐ └───────┌────────────────┐ │ │
* │ │ │──restart()───▶│ connecting │ │ │
* │ │ stopped │ └────────────────┘ │ │
* │ │ │─scheduleReconnect()──│───────────┘ │
* │ └────────────────┘ │ │
* │ ▲ │ │
* │ │ onopen │
* │ │ │ │
* │ │ ▼ │
* terminate() │ ┌────────────────┐ │
* │ └────────stop()─────────│ ready │────────┘
* │ └────────────────┘
* │ │
* │ │
Expand Down Expand Up @@ -127,6 +130,7 @@ export class WebSocketManager {
private readonly onMessage: (message: ServerMessage) => OnMessageResponse;
private readonly webSocketConstructor: typeof WebSocket;
private readonly logger: Logger;
private scheduledReconnectTimerId: NodeJS.Timeout | null;

constructor(
uri: string,
Expand All @@ -149,6 +153,7 @@ export class WebSocketManager {

this.serverInactivityThreshold = 30000;
this.reconnectDueToServerInactivityTimeout = null;
this.scheduledReconnectTimerId = null;

this.uri = uri;
this.onOpen = callbacks.onOpen;
Expand Down Expand Up @@ -176,9 +181,12 @@ export class WebSocketManager {
this.socket.state !== "disconnected" &&
this.socket.state !== "stopped"
) {
throw new Error(
"Didn't start connection from disconnected state: " + this.socket.state,
// This can only happen from a scheduled reconnect kicking off at the
// wrong time. Log the error and bail, but don't throw.
this.logger.error(
`Didn't start connection from disconnected state: ${this.socket.state}, bailing`,
);
return;
}

const ws = new this.webSocketConstructor(this.uri);
Expand Down Expand Up @@ -252,6 +260,8 @@ export class WebSocketManager {
msg += `: ${event.reason}`;
}
this.logger.log(msg);
} else {
this._logVerbose(`WebSocket closed with code ${event.code}`);
}
this.scheduleReconnect();
return;
Expand Down Expand Up @@ -321,10 +331,16 @@ export class WebSocketManager {
}

private scheduleReconnect() {
this.socket = { state: "disconnected" };
this.setSocketState({ state: "disconnected" });
const backoff = this.nextBackoff();
this.logger.log(`Attempting reconnect in ${backoff}ms`);
setTimeout(() => this.connect(), backoff);
if (this.scheduledReconnectTimerId) {
clearTimeout(this.scheduledReconnectTimerId);
}
this.scheduledReconnectTimerId = setTimeout(() => {
this.connect();
this.scheduledReconnectTimerId = null;
}, backoff);
}

/**
Expand All @@ -333,13 +349,15 @@ export class WebSocketManager {
* 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}`);
this._logVerbose(
`begin closeAndReconnect with reason ${closeReason}, socket state: ${this.socket.state}`,
);
switch (this.socket.state) {
case "disconnected":
case "terminated":
case "stopped":
// Nothing to do if we don't have a WebSocket.
// Nothing to do if we're terminating.
return;
case "disconnected":
case "stopped":
case "connecting":
case "ready": {
this.lastCloseReason = closeReason;
Expand All @@ -363,6 +381,7 @@ export class WebSocketManager {
* closed socket is not accessible or used again after this method is called
*/
private close(): Promise<void> {
this._logVerbose(`close attempted with socket state: ${this.socket.state}`);
switch (this.socket.state) {
case "disconnected":
case "terminated":
Expand Down