service: cleanup base implementation and some caller implementations (#7301)

This commit is contained in:
Sam Kleinman
2021-12-01 09:28:06 -05:00
committed by GitHub
parent 3749c37847
commit a823d167bc
28 changed files with 222 additions and 184 deletions
+27 -10
View File
@@ -84,6 +84,7 @@ type BlockPool struct {
requestsCh chan<- BlockRequest
errorsCh chan<- peerError
exitedCh chan struct{}
startHeight int64
lastHundredBlockTimeStamp time.Time
@@ -102,11 +103,11 @@ func NewBlockPool(
bp := &BlockPool{
peers: make(map[types.NodeID]*bpPeer),
requesters: make(map[int64]*bpRequester),
height: start,
startHeight: start,
numPending: 0,
requesters: make(map[int64]*bpRequester),
height: start,
startHeight: start,
numPending: 0,
exitedCh: make(chan struct{}),
requestsCh: requestsCh,
errorsCh: errorsCh,
lastSyncRate: 0,
@@ -121,9 +122,17 @@ func (pool *BlockPool) OnStart(ctx context.Context) error {
pool.lastAdvance = time.Now()
pool.lastHundredBlockTimeStamp = pool.lastAdvance
go pool.makeRequestersRoutine(ctx)
go func() {
defer close(pool.exitedCh)
pool.Wait()
}()
return nil
}
func (*BlockPool) OnStop() {}
// spawns requesters as needed
func (pool *BlockPool) makeRequestersRoutine(ctx context.Context) {
for {
@@ -572,10 +581,12 @@ func newBPRequester(pool *BlockPool, height int64) *bpRequester {
}
func (bpr *bpRequester) OnStart(ctx context.Context) error {
go bpr.requestRoutine()
go bpr.requestRoutine(ctx)
return nil
}
func (*bpRequester) OnStop() {}
// Returns true if the peer matches and block doesn't already exist.
func (bpr *bpRequester) setBlock(block *types.Block, peerID types.NodeID) bool {
bpr.mtx.Lock()
@@ -630,7 +641,13 @@ func (bpr *bpRequester) redo(peerID types.NodeID) {
// Responsible for making more requests as necessary
// Returns only when a block is found (e.g. AddBlock() is called)
func (bpr *bpRequester) requestRoutine() {
func (bpr *bpRequester) requestRoutine(ctx context.Context) {
bprPoolDone := make(chan struct{})
go func() {
defer close(bprPoolDone)
bpr.pool.Wait()
}()
OUTER_LOOP:
for {
// Pick a peer to send request to.
@@ -656,13 +673,13 @@ OUTER_LOOP:
WAIT_LOOP:
for {
select {
case <-bpr.pool.Quit():
case <-ctx.Done():
return
case <-bpr.pool.exitedCh:
if err := bpr.Stop(); err != nil {
bpr.Logger.Error("Error stopped requester", "err", err)
}
return
case <-bpr.Quit():
return
case peerID := <-bpr.redoCh:
if peerID == bpr.peerID {
bpr.reset()
+5 -5
View File
@@ -158,7 +158,7 @@ func (r *Reactor) OnStart(ctx context.Context) error {
return err
}
r.poolWG.Add(1)
go r.requestRoutine()
go r.requestRoutine(ctx)
r.poolWG.Add(1)
go r.poolRoutine(false)
@@ -375,7 +375,7 @@ func (r *Reactor) SwitchToBlockSync(ctx context.Context, state sm.State) error {
r.syncStartTime = time.Now()
r.poolWG.Add(1)
go r.requestRoutine()
go r.requestRoutine(ctx)
r.poolWG.Add(1)
go r.poolRoutine(true)
@@ -383,7 +383,7 @@ func (r *Reactor) SwitchToBlockSync(ctx context.Context, state sm.State) error {
return nil
}
func (r *Reactor) requestRoutine() {
func (r *Reactor) requestRoutine(ctx context.Context) {
statusUpdateTicker := time.NewTicker(statusUpdateIntervalSeconds * time.Second)
defer statusUpdateTicker.Stop()
@@ -394,7 +394,7 @@ func (r *Reactor) requestRoutine() {
case <-r.closeCh:
return
case <-r.pool.Quit():
case <-ctx.Done():
return
case request := <-r.requestsCh:
@@ -607,7 +607,7 @@ FOR_LOOP:
case <-r.closeCh:
break FOR_LOOP
case <-r.pool.Quit():
case <-r.pool.exitedCh:
break FOR_LOOP
}
}