Skip to content

Commit

Permalink
feat(WebSocketShard): heartbeat jitter (#9223)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
didinele and kodiakhq[bot] authored Mar 12, 2023
1 parent 646b6ea commit 6ecff26
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,14 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {

case GatewayOpcodes.Hello: {
this.emit(WebSocketShardEvents.Hello);
this.debug([`Starting to heartbeat every ${payload.d.heartbeat_interval}ms`]);
const jitter = Math.random();
const firstWait = Math.floor(payload.d.heartbeat_interval * jitter);
this.debug([`Preparing first heartbeat of the connection with a jitter of ${jitter}; waiting ${firstWait}ms`]);

await sleep(firstWait);
await this.heartbeat();

this.debug([`First heartbeat sent, starting to beat every ${payload.d.heartbeat_interval}ms`]);
this.heartbeatInterval = setInterval(() => void this.heartbeat(), payload.d.heartbeat_interval);
break;
}
Expand Down

2 comments on commit 6ecff26

@vercel
Copy link

@vercel vercel bot commented on 6ecff26 Mar 12, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 6ecff26 Mar 12, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.