stateroot: wait for the service to stop un Shutdown

This commit is contained in:
Roman Khimov 2022-07-04 10:59:34 +03:00
parent 58b9ac41e2
commit 36d4c17a15
2 changed files with 7 additions and 3 deletions

View file

@ -59,6 +59,7 @@ type (
maxRetries int maxRetries int
relayExtensible RelayCallback relayExtensible RelayCallback
blockCh chan *block.Block blockCh chan *block.Block
stopCh chan struct{}
done chan struct{} done chan struct{}
} }
) )
@ -79,6 +80,7 @@ func New(cfg config.StateRoot, sm *stateroot.Module, log *zap.Logger, bc Ledger,
log: log, log: log,
incompleteRoots: make(map[uint32]*incompleteRoot), incompleteRoots: make(map[uint32]*incompleteRoot),
blockCh: make(chan *block.Block), blockCh: make(chan *block.Block),
stopCh: make(chan struct{}),
done: make(chan struct{}), done: make(chan struct{}),
timePerBlock: time.Duration(bcConf.SecondsPerBlock) * time.Second, timePerBlock: time.Duration(bcConf.SecondsPerBlock) * time.Second,
maxRetries: voteValidEndInc, maxRetries: voteValidEndInc,

View file

@ -46,10 +46,11 @@ runloop:
s.srMtx.Lock() s.srMtx.Lock()
delete(s.incompleteRoots, b.Index-voteValidEndInc) delete(s.incompleteRoots, b.Index-voteValidEndInc)
s.srMtx.Unlock() s.srMtx.Unlock()
case <-s.done: case <-s.stopCh:
break runloop break runloop
} }
} }
s.chain.UnsubscribeFromBlocks(s.blockCh)
drainloop: drainloop:
for { for {
select { select {
@ -59,6 +60,7 @@ drainloop:
} }
} }
close(s.blockCh) close(s.blockCh)
close(s.done)
} }
// Shutdown stops the service. // Shutdown stops the service.
@ -66,8 +68,8 @@ func (s *service) Shutdown() {
if !s.started.CAS(true, false) { if !s.started.CAS(true, false) {
return return
} }
s.chain.UnsubscribeFromBlocks(s.blockCh) close(s.stopCh)
close(s.done) <-s.done
} }
func (s *service) signAndSend(r *state.MPTRoot) error { func (s *service) signAndSend(r *state.MPTRoot) error {