blocksync: Honor contexts supplied to BlockPool (#8447)

* Lift condition into for loop

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Honor contexts in BlockPool

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Only stop timers when necessary

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Optimize timers

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Simplify request interval definition

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Remove extraneous timer stop

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Convert switch into if

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Eliminate timers

Signed-off-by: Thane Thomson <connect@thanethomson.com>
This commit is contained in:
Thane Thomson
2022-04-30 14:34:59 -04:00
committed by GitHub
parent cf2a00b398
commit e7451a43e7
+21 -18
View File
@@ -28,7 +28,7 @@ eg, L = latency = 0.1s
*/ */
const ( const (
requestIntervalMS = 2 requestInterval = 2 * time.Millisecond
maxTotalRequesters = 600 maxTotalRequesters = 600
maxPeerErrBuffer = 1000 maxPeerErrBuffer = 1000
maxPendingRequests = maxTotalRequesters maxPendingRequests = maxTotalRequesters
@@ -130,27 +130,23 @@ func (*BlockPool) OnStop() {}
// spawns requesters as needed // spawns requesters as needed
func (pool *BlockPool) makeRequestersRoutine(ctx context.Context) { func (pool *BlockPool) makeRequestersRoutine(ctx context.Context) {
for { for pool.IsRunning() {
if !pool.IsRunning() { if ctx.Err() != nil {
break return
} }
_, numPending, lenRequesters := pool.GetStatus() _, numPending, lenRequesters := pool.GetStatus()
switch { if numPending >= maxPendingRequests || lenRequesters >= maxTotalRequesters {
case numPending >= maxPendingRequests: // This is preferable to using a timer because the request interval
// sleep for a bit. // is so small. Larger request intervals may necessitate using a
time.Sleep(requestIntervalMS * time.Millisecond) // timer/ticker.
// check for timed out peers time.Sleep(requestInterval)
pool.removeTimedoutPeers() pool.removeTimedoutPeers()
case lenRequesters >= maxTotalRequesters: continue
// sleep for a bit.
time.Sleep(requestIntervalMS * time.Millisecond)
// check for timed out peers
pool.removeTimedoutPeers()
default:
// request for more blocks.
pool.makeNextRequester(ctx)
} }
// request for more blocks.
pool.makeNextRequester(ctx)
} }
} }
@@ -639,9 +635,16 @@ OUTER_LOOP:
if !bpr.IsRunning() || !bpr.pool.IsRunning() { if !bpr.IsRunning() || !bpr.pool.IsRunning() {
return return
} }
if ctx.Err() != nil {
return
}
peer = bpr.pool.pickIncrAvailablePeer(bpr.height) peer = bpr.pool.pickIncrAvailablePeer(bpr.height)
if peer == nil { 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 continue PICK_PEER_LOOP
} }
break PICK_PEER_LOOP break PICK_PEER_LOOP