Skip to content

Commit

Permalink
perf: do not reset the hearbeat timer on each packet
Browse files Browse the repository at this point in the history
This behavior was added in [1]. However, there are two problems:

- a new timer is allocated every time a packet is received, which is
wasteful

- the next heartbeat is not actually delayed, since it's the timeout
timer which gets reset, and not the interval timer

Note: delaying the next heartbeat would be a breaking change.

[1]: be7b4e7
  • Loading branch information
darrachequesne committed Jun 18, 2024
1 parent d3f45dc commit 5359bae
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,14 @@ export class Socket extends EventEmitter {
debug(`received packet ${packet.type}`);
this.emit("packet", packet);

// Reset ping timeout on any packet, incoming data is a good sign of
// other side's liveness
this.resetPingTimeout(
this.server.opts.pingInterval + this.server.opts.pingTimeout
);

switch (packet.type) {
case "ping":
if (this.transport.protocol !== 3) {
this.onError("invalid heartbeat direction");
return;
}
debug("got ping");
this.pingTimeoutTimer.refresh();
this.sendPacket("pong");
this.emit("heartbeat");
break;
Expand All @@ -158,6 +153,7 @@ export class Socket extends EventEmitter {
return;
}
debug("got pong");
clearTimeout(this.pingTimeoutTimer);
this.pingIntervalTimer.refresh();
this.emit("heartbeat");
break;
Expand Down

0 comments on commit 5359bae

Please sign in to comment.