Skip to content

Commit

Permalink
fix: address peering service code feedback
Browse files Browse the repository at this point in the history
* better name for timer
* cancel context from within stop
  • Loading branch information
Stebalien committed May 26, 2020
1 parent 6ba49cf commit 0551c4d
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions peering/peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,22 @@ type peerHandler struct {
ctx context.Context
cancel context.CancelFunc

mu sync.Mutex
addrs []multiaddr.Multiaddr
timer *time.Timer
mu sync.Mutex
addrs []multiaddr.Multiaddr
reconnectTimer *time.Timer

nextDelay time.Duration
}

// stop permanently stops the peer handler.
func (ph *peerHandler) stop() {
ph.cancel()

ph.mu.Lock()
defer ph.mu.Unlock()

if ph.timer != nil {
ph.timer.Stop()
ph.timer = nil
if ph.reconnectTimer != nil {
ph.reconnectTimer.Stop()
ph.reconnectTimer = nil
}
}

Expand All @@ -79,10 +81,10 @@ func (ph *peerHandler) reconnect() {
logger.Debugw("failed to reconnect", "peer", ph.peer, "error", err)
// Ok, we failed. Extend the timeout.
ph.mu.Lock()
if ph.timer != nil {
// Only counts if the timer still exists. If not, a
if ph.reconnectTimer != nil {
// Only counts if the reconnectTimer still exists. If not, a
// connection _was_ somehow established.
ph.timer.Reset(ph.nextBackoff())
ph.reconnectTimer.Reset(ph.nextBackoff())
}
// Otherwise, someone else has stopped us so we can assume that
// we're either connected or someone else will start us.
Expand All @@ -98,10 +100,10 @@ func (ph *peerHandler) stopIfConnected() {
ph.mu.Lock()
defer ph.mu.Unlock()

if ph.timer != nil && ph.host.Network().Connectedness(ph.peer) == network.Connected {
if ph.reconnectTimer != nil && ph.host.Network().Connectedness(ph.peer) == network.Connected {
logger.Debugw("successfully reconnected", "peer", ph.peer)
ph.timer.Stop()
ph.timer = nil
ph.reconnectTimer.Stop()
ph.reconnectTimer = nil
ph.nextDelay = initialDelay
}
}
Expand All @@ -111,10 +113,10 @@ func (ph *peerHandler) startIfDisconnected() {
ph.mu.Lock()
defer ph.mu.Unlock()

if ph.timer == nil && ph.host.Network().Connectedness(ph.peer) != network.Connected {
if ph.reconnectTimer == nil && ph.host.Network().Connectedness(ph.peer) != network.Connected {
logger.Debugw("disconnected from peer", "peer", ph.peer)
// Always start with a short timeout so we can stagger things a bit.
ph.timer = time.AfterFunc(ph.nextBackoff(), ph.reconnect)
ph.reconnectTimer = time.AfterFunc(ph.nextBackoff(), ph.reconnect)
}
}

Expand Down Expand Up @@ -222,7 +224,6 @@ func (ps *PeeringService) RemovePeer(id peer.ID) {
ps.host.ConnManager().Unprotect(id, connmgrTag)

handler.stop()
handler.cancel()
delete(ps.peers, id)
}
}
Expand Down

0 comments on commit 0551c4d

Please sign in to comment.