@@ -65,6 +65,7 @@ type Hive struct {
65
65
lock sync.Mutex
66
66
peers map [enode.ID ]* BzzPeer
67
67
ticker * time.Ticker
68
+ done chan struct {}
68
69
}
69
70
70
71
// NewHive constructs a new hive
@@ -97,6 +98,8 @@ func (h *Hive) Start(server *p2p.Server) error {
97
98
h .addPeer = server .AddPeer
98
99
// ticker to keep the hive alive
99
100
h .ticker = time .NewTicker (h .KeepAliveInterval )
101
+ // done channel to signal the connect goroutine to return after Stop
102
+ h .done = make (chan struct {})
100
103
// this loop is doing bootstrapping and maintains a healthy table
101
104
if ! h .DisableAutoConnect {
102
105
go h .connect ()
@@ -108,6 +111,7 @@ func (h *Hive) Start(server *p2p.Server) error {
108
111
func (h * Hive ) Stop () error {
109
112
log .Info (fmt .Sprintf ("%08x hive stopping, saving peers" , h .BaseAddr ()[:4 ]))
110
113
h .ticker .Stop ()
114
+ close (h .done )
111
115
if h .Store != nil {
112
116
if err := h .savePeers (); err != nil {
113
117
return fmt .Errorf ("could not save peers to persistence store: %v" , err )
@@ -131,24 +135,29 @@ func (h *Hive) Stop() error {
131
135
// at each iteration, ask the overlay driver to suggest the most preferred peer to connect to
132
136
// as well as advertises saturation depth if needed
133
137
func (h * Hive ) connect () {
134
- for range h .ticker .C {
138
+ loop:
139
+ for {
140
+ select {
141
+ case <- h .ticker .C :
142
+ addr , depth , changed := h .SuggestPeer ()
143
+ if h .Discovery && changed {
144
+ NotifyDepth (uint8 (depth ), h .Kademlia )
145
+ }
146
+ if addr == nil {
147
+ continue loop
148
+ }
135
149
136
- addr , depth , changed := h .SuggestPeer ()
137
- if h .Discovery && changed {
138
- NotifyDepth (uint8 (depth ), h .Kademlia )
150
+ log .Trace (fmt .Sprintf ("%08x hive connect() suggested %08x" , h .BaseAddr ()[:4 ], addr .Address ()[:4 ]))
151
+ under , err := enode .ParseV4 (string (addr .Under ()))
152
+ if err != nil {
153
+ log .Warn (fmt .Sprintf ("%08x unable to connect to bee %08x: invalid node URL: %v" , h .BaseAddr ()[:4 ], addr .Address ()[:4 ], err ))
154
+ continue loop
155
+ }
156
+ log .Trace (fmt .Sprintf ("%08x attempt to connect to bee %08x" , h .BaseAddr ()[:4 ], addr .Address ()[:4 ]))
157
+ h .addPeer (under )
158
+ case <- h .done :
159
+ break loop
139
160
}
140
- if addr == nil {
141
- continue
142
- }
143
-
144
- log .Trace (fmt .Sprintf ("%08x hive connect() suggested %08x" , h .BaseAddr ()[:4 ], addr .Address ()[:4 ]))
145
- under , err := enode .ParseV4 (string (addr .Under ()))
146
- if err != nil {
147
- log .Warn (fmt .Sprintf ("%08x unable to connect to bee %08x: invalid node URL: %v" , h .BaseAddr ()[:4 ], addr .Address ()[:4 ], err ))
148
- continue
149
- }
150
- log .Trace (fmt .Sprintf ("%08x attempt to connect to bee %08x" , h .BaseAddr ()[:4 ], addr .Address ()[:4 ]))
151
- h .addPeer (under )
152
161
}
153
162
}
154
163
0 commit comments