diff --git a/packages/autopeering/discover/protocol.go b/packages/autopeering/discover/protocol.go index 8fbda7d71422dbb5fc6472d9128846a9a6dd14ff..07790b9467f1e7ee1406bb867910a9c267b043fa 100644 --- a/packages/autopeering/discover/protocol.go +++ b/packages/autopeering/discover/protocol.go @@ -15,6 +15,7 @@ import ( "github.com/iotaledger/goshimmer/packages/autopeering/server" "github.com/iotaledger/hive.go/backoff" "github.com/iotaledger/hive.go/logger" + "github.com/iotaledger/hive.go/typeutils" ) const ( @@ -35,6 +36,7 @@ type Protocol struct { log *logger.Logger // logging mgr *manager // the manager handles the actual peer discovery and re-verification + running *typeutils.AtomicBool closeOnce sync.Once } @@ -44,6 +46,7 @@ func New(local *peer.Local, cfg Config) *Protocol { Protocol: server.Protocol{}, loc: local, log: cfg.Log, + running: typeutils.NewAtomicBool(), } p.mgr = newManager(p, cfg.MasterPeers, cfg.Log.Named("mgr")) @@ -54,13 +57,14 @@ func New(local *peer.Local, cfg Config) *Protocol { func (p *Protocol) Start(s server.Sender) { p.Protocol.Sender = s p.mgr.start() - p.log.Debug("discover started") + p.running.Set() } // Close finalizes the protocol. func (p *Protocol) Close() { p.closeOnce.Do(func() { + p.running.UnSet() p.mgr.close() }) } @@ -105,8 +109,11 @@ func (p *Protocol) GetVerifiedPeers() []*peer.Peer { // HandleMessage responds to incoming peer discovery messages. func (p *Protocol) HandleMessage(s *server.Server, fromAddr string, fromID peer.ID, fromKey peer.PublicKey, data []byte) (bool, error) { - switch pb.MType(data[0]) { + if !p.running.IsSet() { + return false, nil + } + switch pb.MType(data[0]) { // Ping case pb.MPing: m := new(pb.Ping) diff --git a/packages/autopeering/selection/protocol.go b/packages/autopeering/selection/protocol.go index 61ad178d8b46ad5b55e9d85c647e9de9ea85fa9a..b0cf1cc1186f51690a7693e0565b7eb4d06f7a06 100644 --- a/packages/autopeering/selection/protocol.go +++ b/packages/autopeering/selection/protocol.go @@ -15,6 +15,7 @@ import ( "github.com/iotaledger/goshimmer/packages/autopeering/server" "github.com/iotaledger/hive.go/backoff" "github.com/iotaledger/hive.go/logger" + "github.com/iotaledger/hive.go/typeutils" ) const ( @@ -45,6 +46,7 @@ type Protocol struct { log *logger.Logger // logging mgr *manager // the manager handles the actual neighbor selection + running *typeutils.AtomicBool closeOnce sync.Once } @@ -55,6 +57,7 @@ func New(local *peer.Local, disc DiscoverProtocol, cfg Config) *Protocol { loc: local, disc: disc, log: cfg.Log, + running: typeutils.NewAtomicBool(), } p.mgr = newManager(p, disc.GetVerifiedPeers, cfg.Log.Named("mgr"), cfg) @@ -65,13 +68,14 @@ func New(local *peer.Local, disc DiscoverProtocol, cfg Config) *Protocol { func (p *Protocol) Start(s server.Sender) { p.Protocol.Sender = s p.mgr.start() - p.log.Debug("neighborhood started") + p.running.Set() } // Close finalizes the protocol. func (p *Protocol) Close() { p.closeOnce.Do(func() { + p.running.UnSet() p.mgr.close() }) } @@ -100,8 +104,11 @@ func (p *Protocol) RemoveNeighbor(id peer.ID) { // HandleMessage responds to incoming neighbor selection messages. func (p *Protocol) HandleMessage(s *server.Server, fromAddr string, fromID peer.ID, _ peer.PublicKey, data []byte) (bool, error) { - switch pb.MType(data[0]) { + if !p.running.IsSet() { + return false, nil + } + switch pb.MType(data[0]) { // PeeringRequest case pb.MPeeringRequest: m := new(pb.PeeringRequest)