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

Lines changed: 2 additions & 6 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy