From 569933316eca77c26de354c590c5ce63ae3dbbaf Mon Sep 17 00:00:00 2001 From: tuyennhv Date: Mon, 19 Jun 2023 15:24:34 +0700 Subject: [PATCH] fix: gossipsub to yield more to the macro queue (#5664) * fix: add setTimeout to onGossipsubMessage and onValidationResult * chore: more comments --- .../src/network/gossip/gossipsub.ts | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/beacon-node/src/network/gossip/gossipsub.ts b/packages/beacon-node/src/network/gossip/gossipsub.ts index fbafabcf1a44..ef159e019077 100644 --- a/packages/beacon-node/src/network/gossip/gossipsub.ts +++ b/packages/beacon-node/src/network/gossip/gossipsub.ts @@ -279,21 +279,30 @@ export class Eth2Gossipsub extends GossipSub { // Get seenTimestamp before adding the message to the queue or add async delays const seenTimestampSec = Date.now() / 1000; - // Emit message to network processor - this.events.emit(NetworkEvent.pendingGossipsubMessage, { - topic, - msg, - msgId, - // Hot path, use cached .toString() version - propagationSource: propagationSource.toString(), - seenTimestampSec, - startProcessUnixSec: null, - }); + // Use setTimeout to yield to the macro queue + // Without this we'll have huge event loop lag + // See https://github.com/ChainSafe/lodestar/issues/5604 + setTimeout(() => { + this.events.emit(NetworkEvent.pendingGossipsubMessage, { + topic, + msg, + msgId, + // Hot path, use cached .toString() version + propagationSource: propagationSource.toString(), + seenTimestampSec, + startProcessUnixSec: null, + }); + }, 0); } private onValidationResult(data: NetworkEventData[NetworkEvent.gossipMessageValidationResult]): void { - // TODO: reportMessageValidationResult should take PeerIdStr since it only uses string version - this.reportMessageValidationResult(data.msgId, peerIdFromString(data.propagationSource), data.acceptance); + // Use setTimeout to yield to the macro queue + // Without this we'll have huge event loop lag + // See https://github.com/ChainSafe/lodestar/issues/5604 + setTimeout(() => { + // TODO: reportMessageValidationResult should take PeerIdStr since it only uses string version + this.reportMessageValidationResult(data.msgId, peerIdFromString(data.propagationSource), data.acceptance); + }, 0); } }