From e7451a43e72ea5cd0cbaba605b8da3269ee3e8a0 Mon Sep 17 00:00:00 2001 From: Thane Thomson Date: Sat, 30 Apr 2022 14:34:59 -0400 Subject: [PATCH] blocksync: Honor contexts supplied to BlockPool (#8447) * Lift condition into for loop Signed-off-by: Thane Thomson * Honor contexts in BlockPool Signed-off-by: Thane Thomson * Only stop timers when necessary Signed-off-by: Thane Thomson * Optimize timers Signed-off-by: Thane Thomson * Simplify request interval definition Signed-off-by: Thane Thomson * Remove extraneous timer stop Signed-off-by: Thane Thomson * Convert switch into if Signed-off-by: Thane Thomson * Eliminate timers Signed-off-by: Thane Thomson --- internal/blocksync/pool.go | 39 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/internal/blocksync/pool.go b/internal/blocksync/pool.go index 7f133a7a1..f00a2fab5 100644 --- a/internal/blocksync/pool.go +++ b/internal/blocksync/pool.go @@ -28,7 +28,7 @@ eg, L = latency = 0.1s */ const ( - requestIntervalMS = 2 + requestInterval = 2 * time.Millisecond maxTotalRequesters = 600 maxPeerErrBuffer = 1000 maxPendingRequests = maxTotalRequesters @@ -130,27 +130,23 @@ func (*BlockPool) OnStop() {} // spawns requesters as needed func (pool *BlockPool) makeRequestersRoutine(ctx context.Context) { - for { - if !pool.IsRunning() { - break + for pool.IsRunning() { + if ctx.Err() != nil { + return } _, numPending, lenRequesters := pool.GetStatus() - switch { - case numPending >= maxPendingRequests: - // sleep for a bit. - time.Sleep(requestIntervalMS * time.Millisecond) - // check for timed out peers + if numPending >= maxPendingRequests || lenRequesters >= maxTotalRequesters { + // This is preferable to using a timer because the request interval + // is so small. Larger request intervals may necessitate using a + // timer/ticker. + time.Sleep(requestInterval) pool.removeTimedoutPeers() - case lenRequesters >= maxTotalRequesters: - // sleep for a bit. - time.Sleep(requestIntervalMS * time.Millisecond) - // check for timed out peers - pool.removeTimedoutPeers() - default: - // request for more blocks. - pool.makeNextRequester(ctx) + continue } + + // request for more blocks. + pool.makeNextRequester(ctx) } } @@ -639,9 +635,16 @@ OUTER_LOOP: if !bpr.IsRunning() || !bpr.pool.IsRunning() { return } + if ctx.Err() != nil { + return + } + peer = bpr.pool.pickIncrAvailablePeer(bpr.height) if peer == nil { - time.Sleep(requestIntervalMS * time.Millisecond) + // This is preferable to using a timer because the request + // interval is so small. Larger request intervals may + // necessitate using a timer/ticker. + time.Sleep(requestInterval) continue PICK_PEER_LOOP } break PICK_PEER_LOOP