Skip to content

Commit

Permalink
probably caused a race condition, but will test with the test setup b…
Browse files Browse the repository at this point in the history
…efore fixing
  • Loading branch information
faddat committed Feb 25, 2024
1 parent 71078c2 commit f4bbffb
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions blocksync/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit f4bbffb

Please sign in to comment.