fix: address peering service code feedback
* better name for timer * cancel context from within stop
Steven Allen committed
May 25, 2020 at 19:11 UTC
0551c4dca843fe3a9665fdc49cd2dcecaeb85046
1 file changed
+17
-16
peering/peering.go
+17
-16
@@ -40,20 +40,22 @@ type peerHandler struct {
40
ctx context.Context
41
cancel context.CancelFunc
42
43
- mu sync.Mutex
44
- addrs []multiaddr.Multiaddr
45
- timer *time.Timer
43
+ mu sync.Mutex
44
+ addrs []multiaddr.Multiaddr
45
+ reconnectTimer *time.Timer
46
47
nextDelay time.Duration
48
}
49
50
+// stop permanently stops the peer handler.
51
func (ph *peerHandler) stop() {
52
+ ph.cancel()
53
+
54
ph.mu.Lock()
55
defer ph.mu.Unlock()
53
-
54
- if ph.timer != nil {
55
- ph.timer.Stop()
56
- ph.timer = nil
56
+ if ph.reconnectTimer != nil {
57
+ ph.reconnectTimer.Stop()
58
+ ph.reconnectTimer = nil
59
}
60
}
61
@@ -79,10 +81,10 @@ func (ph *peerHandler) reconnect() {
81
logger.Debugw("failed to reconnect", "peer", ph.peer, "error", err)
82
// Ok, we failed. Extend the timeout.
83
ph.mu.Lock()
82
- if ph.timer != nil {
83
- // Only counts if the timer still exists. If not, a
84
+ if ph.reconnectTimer != nil {
85
+ // Only counts if the reconnectTimer still exists. If not, a
86
// connection _was_ somehow established.
85
- ph.timer.Reset(ph.nextBackoff())
87
+ ph.reconnectTimer.Reset(ph.nextBackoff())
88
}
89
// Otherwise, someone else has stopped us so we can assume that
90
// we're either connected or someone else will start us.
@@ -98,10 +100,10 @@ func (ph *peerHandler) stopIfConnected() {
100
ph.mu.Lock()
101
defer ph.mu.Unlock()
102
101
- if ph.timer != nil && ph.host.Network().Connectedness(ph.peer) == network.Connected {
103
+ if ph.reconnectTimer != nil && ph.host.Network().Connectedness(ph.peer) == network.Connected {
104
logger.Debugw("successfully reconnected", "peer", ph.peer)
103
- ph.timer.Stop()
104
- ph.timer = nil
105
+ ph.reconnectTimer.Stop()
106
+ ph.reconnectTimer = nil
107
ph.nextDelay = initialDelay
108
}
109
}
@@ -111,10 +113,10 @@ func (ph *peerHandler) startIfDisconnected() {
113
ph.mu.Lock()
114
defer ph.mu.Unlock()
115
114
- if ph.timer == nil && ph.host.Network().Connectedness(ph.peer) != network.Connected {
116
+ if ph.reconnectTimer == nil && ph.host.Network().Connectedness(ph.peer) != network.Connected {
117
logger.Debugw("disconnected from peer", "peer", ph.peer)
118
// Always start with a short timeout so we can stagger things a bit.
117
- ph.timer = time.AfterFunc(ph.nextBackoff(), ph.reconnect)
119
+ ph.reconnectTimer = time.AfterFunc(ph.nextBackoff(), ph.reconnect)
120
}
121
}
122
@@ -222,7 +224,6 @@ func (ps *PeeringService) RemovePeer(id peer.ID) {
224
ps.host.ConnManager().Unprotect(id, connmgrTag)
225
226
handler.stop()
225
- handler.cancel()
227
delete(ps.peers, id)
228
}
229
}