From 75e4c26564c257836ad2001797900bf385c7ba9a Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Mon, 24 Mar 2025 03:53:27 +0100 Subject: [PATCH] p2p/connmanager: avoid dropping peers too early Signed-off-by: Csaba Kiraly --- p2p/connmanager.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/p2p/connmanager.go b/p2p/connmanager.go index 634b4805df..574220e8d9 100644 --- a/p2p/connmanager.go +++ b/p2p/connmanager.go @@ -30,6 +30,8 @@ import ( const ( // Interval between peer drop events peerDropInterval = 30 * time.Second + // Avoid dropping peers for some time after connection + doNotDropBefore = 2 * peerDropInterval // How close to max should we initiate the drop timer. O should be fine, // dropping when no more peers can be added. Larger numbers result in more // aggressive drop behavior. @@ -133,8 +135,10 @@ func (cm *connManager) dropRandomPeer() bool { peers := cm.peersFunc() // Only drop from dyndialed peers. Avoid dropping trusted peers. + // Give some time to peers before considering them for a drop. selectDroppable := func(p *Peer) bool { - return p.rw.is(dynDialedConn) && !p.rw.is(trustedConn) + return p.rw.is(dynDialedConn) && !p.rw.is(trustedConn) && + mclock.Now()-p.created >= mclock.AbsTime(doNotDropBefore) } droppable := filter(peers, selectDroppable) if len(droppable) > 0 {