forked from TrueCloudLab/neoneo-go
consensus: allow to shutdown service
This commit is contained in:
parent
0dda247719
commit
282b55494b
2 changed files with 15 additions and 0 deletions
|
@ -42,6 +42,8 @@ type Service interface {
|
||||||
// Start initializes dBFT and starts event loop for consensus service.
|
// Start initializes dBFT and starts event loop for consensus service.
|
||||||
// It must be called only when sufficient amount of peers are connected.
|
// It must be called only when sufficient amount of peers are connected.
|
||||||
Start()
|
Start()
|
||||||
|
// Shutdown stops dBFT event loop.
|
||||||
|
Shutdown()
|
||||||
|
|
||||||
// OnPayload is a callback to notify Service about new received payload.
|
// OnPayload is a callback to notify Service about new received payload.
|
||||||
OnPayload(p *Payload)
|
OnPayload(p *Payload)
|
||||||
|
@ -73,6 +75,7 @@ type service struct {
|
||||||
// started is a flag set with Start method that runs an event handling
|
// started is a flag set with Start method that runs an event handling
|
||||||
// goroutine.
|
// goroutine.
|
||||||
started *atomic.Bool
|
started *atomic.Bool
|
||||||
|
quit chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config is a configuration for consensus services.
|
// Config is a configuration for consensus services.
|
||||||
|
@ -115,6 +118,7 @@ func NewService(cfg Config) (Service, error) {
|
||||||
blockEvents: make(chan *coreb.Block, 1),
|
blockEvents: make(chan *coreb.Block, 1),
|
||||||
network: cfg.Chain.GetConfig().Magic,
|
network: cfg.Chain.GetConfig().Magic,
|
||||||
started: atomic.NewBool(false),
|
started: atomic.NewBool(false),
|
||||||
|
quit: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Wallet == nil {
|
if cfg.Wallet == nil {
|
||||||
|
@ -190,9 +194,17 @@ func (s *service) Start() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shutdown implements Service interface.
|
||||||
|
func (s *service) Shutdown() {
|
||||||
|
close(s.quit)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *service) eventLoop() {
|
func (s *service) eventLoop() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-s.quit:
|
||||||
|
s.dbft.Timer.Stop()
|
||||||
|
return
|
||||||
case <-s.dbft.Timer.C():
|
case <-s.dbft.Timer.C():
|
||||||
hv := s.dbft.Timer.HV()
|
hv := s.dbft.Timer.HV()
|
||||||
s.log.Debug("timer fired",
|
s.log.Debug("timer fired",
|
||||||
|
|
|
@ -187,6 +187,9 @@ func (s *Server) Shutdown() {
|
||||||
s.log.Info("shutting down server", zap.Int("peers", s.PeerCount()))
|
s.log.Info("shutting down server", zap.Int("peers", s.PeerCount()))
|
||||||
s.transport.Close()
|
s.transport.Close()
|
||||||
s.discovery.Close()
|
s.discovery.Close()
|
||||||
|
if s.consensusStarted.Load() {
|
||||||
|
s.consensus.Shutdown()
|
||||||
|
}
|
||||||
for p := range s.Peers() {
|
for p := range s.Peers() {
|
||||||
p.Disconnect(errServerShutdown)
|
p.Disconnect(errServerShutdown)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue