@cryptotaxi247 / kubo / commits / 17b3b0254

fix: remove unecessary context

Steven Allen committed May 25, 2020 at 20:20 UTC 17b3b02549ef96534b4d170674d4ccf174efb1d1
1 file changed +13 -11
peering/peering.go
+13 -11
@@ -141,18 +141,13 @@ type PeeringService struct {
141
142 mu sync.RWMutex
143 peers map[peer.ID]*peerHandler
144 -
145 - ctx context.Context
146 - cancel context.CancelFunc
147 - state state
144 + state state
145 }
146
147 // NewPeeringService constructs a new peering service. Peers can be added and
148 // removed immediately, but connections won't be formed until `Start` is called.
149 func NewPeeringService(host host.Host) *PeeringService {
153 - ps := &PeeringService{host: host, peers: make(map[peer.ID]*peerHandler)}
154 - ps.ctx, ps.cancel = context.WithCancel(context.Background())
155 - return ps
150 + return &PeeringService{host: host, peers: make(map[peer.ID]*peerHandler)}
151 }
152
153 // Start starts the peering service, connecting and maintaining connections to
@@ -180,17 +175,18 @@ func (ps *PeeringService) Start() error {
175
176 // Stop stops the peering service.
177 func (ps *PeeringService) Stop() error {
183 - ps.cancel()
178 ps.host.Network().StopNotify((*netNotifee)(ps))
179
180 ps.mu.Lock()
181 defer ps.mu.Unlock()
182
189 - if ps.state == stateRunning {
183 + switch ps.state {
184 + case stateInit, stateRunning:
185 logger.Infow("stopping")
186 for _, handler := range ps.peers {
187 handler.stop()
188 }
189 + ps.state = stateStopped
190 }
191 return nil
192 }
@@ -218,10 +214,16 @@ func (ps *PeeringService) AddPeer(info peer.AddrInfo) {
214 addrs: info.Addrs,
215 nextDelay: initialDelay,
216 }
221 - handler.ctx, handler.cancel = context.WithCancel(ps.ctx)
217 + handler.ctx, handler.cancel = context.WithCancel(context.Background())
218 ps.peers[info.ID] = handler
223 - if ps.state == stateRunning {
219 + switch ps.state {
220 + case stateRunning:
221 go handler.startIfDisconnected()
222 + case stateStopped:
223 + // We still construct everything in this state because
224 + // it's easier to reason about. But we should still free
225 + // resources.
226 + handler.cancel()
227 }
228 }
229 }