Skip to content
Snippets Groups Projects
Unverified Commit da5e87ba authored by Angelo Capossele's avatar Angelo Capossele Committed by GitHub
Browse files

Merge pull request #179 from iotaledger/fix/autopeering-race

Fix: Race condition in autopeering
parents 83fc6010 80a2612c
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment