From 5359bae683e2a25742bd4989d0355a8fc10d294e Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 18 Jun 2024 17:14:41 +0200 Subject: [PATCH] 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]: https://github.com/socketio/engine.io/commit/be7b4e7478132a9409603327b27d1aa1970dd1d9 --- lib/socket.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/socket.ts b/lib/socket.ts index 300fcb19..8c35b7a3 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -135,12 +135,6 @@ 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) { @@ -148,6 +142,7 @@ export class Socket extends EventEmitter { return; } debug("got ping"); + this.pingTimeoutTimer.refresh(); this.sendPacket("pong"); this.emit("heartbeat"); break; @@ -158,6 +153,7 @@ export class Socket extends EventEmitter { return; } debug("got pong"); + clearTimeout(this.pingTimeoutTimer); this.pingIntervalTimer.refresh(); this.emit("heartbeat"); break;