Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
network: terminate Hive connect goroutine on Stop (#1740)
Browse files Browse the repository at this point in the history
  • Loading branch information
janos authored Sep 10, 2019
1 parent b9ce9f7 commit 43f2b87
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions network/hive.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Hive struct {
lock sync.Mutex
peers map[enode.ID]*BzzPeer
ticker *time.Ticker
done chan struct{}
}

// NewHive constructs a new hive
Expand Down Expand Up @@ -97,6 +98,8 @@ func (h *Hive) Start(server *p2p.Server) error {
h.addPeer = server.AddPeer
// ticker to keep the hive alive
h.ticker = time.NewTicker(h.KeepAliveInterval)
// done channel to signal the connect goroutine to return after Stop
h.done = make(chan struct{})
// this loop is doing bootstrapping and maintains a healthy table
if !h.DisableAutoConnect {
go h.connect()
Expand All @@ -108,6 +111,7 @@ func (h *Hive) Start(server *p2p.Server) error {
func (h *Hive) Stop() error {
log.Info(fmt.Sprintf("%08x hive stopping, saving peers", h.BaseAddr()[:4]))
h.ticker.Stop()
close(h.done)
if h.Store != nil {
if err := h.savePeers(); err != nil {
return fmt.Errorf("could not save peers to persistence store: %v", err)
Expand All @@ -131,24 +135,29 @@ func (h *Hive) Stop() error {
// at each iteration, ask the overlay driver to suggest the most preferred peer to connect to
// as well as advertises saturation depth if needed
func (h *Hive) connect() {
for range h.ticker.C {
loop:
for {
select {
case <-h.ticker.C:
addr, depth, changed := h.SuggestPeer()
if h.Discovery && changed {
NotifyDepth(uint8(depth), h.Kademlia)
}
if addr == nil {
continue loop
}

addr, depth, changed := h.SuggestPeer()
if h.Discovery && changed {
NotifyDepth(uint8(depth), h.Kademlia)
log.Trace(fmt.Sprintf("%08x hive connect() suggested %08x", h.BaseAddr()[:4], addr.Address()[:4]))
under, err := enode.ParseV4(string(addr.Under()))
if err != nil {
log.Warn(fmt.Sprintf("%08x unable to connect to bee %08x: invalid node URL: %v", h.BaseAddr()[:4], addr.Address()[:4], err))
continue loop
}
log.Trace(fmt.Sprintf("%08x attempt to connect to bee %08x", h.BaseAddr()[:4], addr.Address()[:4]))
h.addPeer(under)
case <-h.done:
break loop
}
if addr == nil {
continue
}

log.Trace(fmt.Sprintf("%08x hive connect() suggested %08x", h.BaseAddr()[:4], addr.Address()[:4]))
under, err := enode.ParseV4(string(addr.Under()))
if err != nil {
log.Warn(fmt.Sprintf("%08x unable to connect to bee %08x: invalid node URL: %v", h.BaseAddr()[:4], addr.Address()[:4], err))
continue
}
log.Trace(fmt.Sprintf("%08x attempt to connect to bee %08x", h.BaseAddr()[:4], addr.Address()[:4]))
h.addPeer(under)
}
}

Expand Down

0 comments on commit 43f2b87

Please sign in to comment.