From f4bbffbcee46ceb5a99a712fd3c4f7a58ecf1293 Mon Sep 17 00:00:00 2001 From: Jacob Gadikian Date: Sun, 25 Feb 2024 21:49:18 +0200 Subject: [PATCH] probably caused a race condition, but will test with the test setup before fixing --- blocksync/pool.go | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/blocksync/pool.go b/blocksync/pool.go index 08dfd1746df..6920d3ca9e2 100644 --- a/blocksync/pool.go +++ b/blocksync/pool.go @@ -427,21 +427,31 @@ func (pool *BlockPool) pickIncrAvailablePeer(height int64) *bpPeer { pool.mtx.Lock() defer pool.mtx.Unlock() + var bestPeer *bpPeer + var bestThroughput int64 + for _, peer := range pool.peers { - if peer.didTimeout { - pool.removePeer(peer.id) - continue - } - if peer.numPending >= maxPendingRequestsPerPeer { + if peer.didTimeout || peer.numPending >= maxPendingRequestsPerPeer { continue } if height < peer.base || height > peer.height { continue } - peer.incrPending() - return peer + // Check if recvMonitor is not nil before accessing it + if peer.recvMonitor != nil { + throughput := peer.recvMonitor.Status().CurRate + if bestPeer == nil || throughput > bestThroughput { + bestPeer = peer + bestThroughput = throughput + } + } } - return nil + + if bestPeer != nil { + bestPeer.incrPending() + } + + return bestPeer } func (pool *BlockPool) makeNextRequester() { @@ -528,12 +538,13 @@ type bpPeer struct { func newBPPeer(pool *BlockPool, peerID p2p.ID, base int64, height int64) *bpPeer { peer := &bpPeer{ - pool: pool, - id: peerID, - base: base, - height: height, - numPending: 0, - logger: log.NewNopLogger(), + pool: pool, + id: peerID, + base: base, + height: height, + numPending: 0, + logger: log.NewNopLogger(), + recvMonitor: flow.New(time.Second, time.Second*40), // Ensure recvMonitor is initialized } return peer }