Skip to content

Commit 5359bae

Browse files
perf: do not reset the hearbeat timer on each packet
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
1 parent d3f45dc commit 5359bae

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

lib/socket.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,14 @@ export class Socket extends EventEmitter {
135135
debug(`received packet ${packet.type}`);
136136
this.emit("packet", packet);
137137

138-
// Reset ping timeout on any packet, incoming data is a good sign of
139-
// other side's liveness
140-
this.resetPingTimeout(
141-
this.server.opts.pingInterval + this.server.opts.pingTimeout
142-
);
143-
144138
switch (packet.type) {
145139
case "ping":
146140
if (this.transport.protocol !== 3) {
147141
this.onError("invalid heartbeat direction");
148142
return;
149143
}
150144
debug("got ping");
145+
this.pingTimeoutTimer.refresh();
151146
this.sendPacket("pong");
152147
this.emit("heartbeat");
153148
break;
@@ -158,6 +153,7 @@ export class Socket extends EventEmitter {
158153
return;
159154
}
160155
debug("got pong");
156+
clearTimeout(this.pingTimeoutTimer);
161157
this.pingIntervalTimer.refresh();
162158
this.emit("heartbeat");
163159
break;

0 commit comments

Comments
 (0)