Skip to content

Commit e7451a4

Browse files
authored
blocksync: Honor contexts supplied to BlockPool (tendermint#8447)
* Lift condition into for loop Signed-off-by: Thane Thomson <[email protected]> * Honor contexts in BlockPool Signed-off-by: Thane Thomson <[email protected]> * Only stop timers when necessary Signed-off-by: Thane Thomson <[email protected]> * Optimize timers Signed-off-by: Thane Thomson <[email protected]> * Simplify request interval definition Signed-off-by: Thane Thomson <[email protected]> * Remove extraneous timer stop Signed-off-by: Thane Thomson <[email protected]> * Convert switch into if Signed-off-by: Thane Thomson <[email protected]> * Eliminate timers Signed-off-by: Thane Thomson <[email protected]>
1 parent cf2a00b commit e7451a4

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

internal/blocksync/pool.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ eg, L = latency = 0.1s
2828
*/
2929

3030
const (
31-
requestIntervalMS = 2
31+
requestInterval = 2 * time.Millisecond
3232
maxTotalRequesters = 600
3333
maxPeerErrBuffer = 1000
3434
maxPendingRequests = maxTotalRequesters
@@ -130,27 +130,23 @@ func (*BlockPool) OnStop() {}
130130

131131
// spawns requesters as needed
132132
func (pool *BlockPool) makeRequestersRoutine(ctx context.Context) {
133-
for {
134-
if !pool.IsRunning() {
135-
break
133+
for pool.IsRunning() {
134+
if ctx.Err() != nil {
135+
return
136136
}
137137

138138
_, numPending, lenRequesters := pool.GetStatus()
139-
switch {
140-
case numPending >= maxPendingRequests:
141-
// sleep for a bit.
142-
time.Sleep(requestIntervalMS * time.Millisecond)
143-
// check for timed out peers
144-
pool.removeTimedoutPeers()
145-
case lenRequesters >= maxTotalRequesters:
146-
// sleep for a bit.
147-
time.Sleep(requestIntervalMS * time.Millisecond)
148-
// check for timed out peers
139+
if numPending >= maxPendingRequests || lenRequesters >= maxTotalRequesters {
140+
// This is preferable to using a timer because the request interval
141+
// is so small. Larger request intervals may necessitate using a
142+
// timer/ticker.
143+
time.Sleep(requestInterval)
149144
pool.removeTimedoutPeers()
150-
default:
151-
// request for more blocks.
152-
pool.makeNextRequester(ctx)
145+
continue
153146
}
147+
148+
// request for more blocks.
149+
pool.makeNextRequester(ctx)
154150
}
155151
}
156152

@@ -639,9 +635,16 @@ OUTER_LOOP:
639635
if !bpr.IsRunning() || !bpr.pool.IsRunning() {
640636
return
641637
}
638+
if ctx.Err() != nil {
639+
return
640+
}
641+
642642
peer = bpr.pool.pickIncrAvailablePeer(bpr.height)
643643
if peer == nil {
644-
time.Sleep(requestIntervalMS * time.Millisecond)
644+
// This is preferable to using a timer because the request
645+
// interval is so small. Larger request intervals may
646+
// necessitate using a timer/ticker.
647+
time.Sleep(requestInterval)
645648
continue PICK_PEER_LOOP
646649
}
647650
break PICK_PEER_LOOP

0 commit comments

Comments
 (0)