Skip to content

[server] make ping pong async #8348

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

Merged
merged 1 commit into from
Feb 21, 2022
Merged
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
21 changes: 18 additions & 3 deletions components/server/src/express/ws-connection-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,16 @@ export class WsConnectionHandler implements Disposable {
}
// if no ping was sent, yet, this is a fresh ws connection

setPingSent(ws, Date.now());
ws.ping(); // if this fails it triggers a ws error, and fails the ws anyway

// note: decoupling by using `setImmediate` in order to offload to the following event loop iteration.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

setImmediate(() => {
try {
ws.ping(); // if this fails it triggers a ws error, and fails the ws anyway
setPingSent(ws, Date.now());
} catch (err) {
log.error("websocket ping error", err);
}
});
} catch (err) {
log.error("websocket ping-pong error", err);
}
Expand All @@ -93,7 +101,14 @@ export class WsConnectionHandler implements Disposable {
});
ws.on('ping', (data: any) => {
// answer browser-side ping to conform RFC6455 (https://tools.ietf.org/html/rfc6455#section-5.5.2)
ws.pong(data);
// note: decoupling by using `setImmediate` in order to offload to the following event loop iteration.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

setImmediate(() => {
try {
ws.pong(data);
} catch (err) {
log.error("websocket pong error", err);
}
});
});

// error handling
Expand Down