From 38b52df51fbc68df846628c59759c914a9e9aa67 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Fri, 15 Jun 2018 12:33:24 -0700 Subject: [PATCH] peer+server: remove persistent connections for peers with no open channels remaining --- peer.go | 18 +++++++++++++++--- server.go | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/peer.go b/peer.go index 1d01e7bf6..2816e037f 100644 --- a/peer.go +++ b/peer.go @@ -1692,9 +1692,6 @@ func (p *peer) fetchActiveChanCloser(chanID lnwire.ChannelID) (*channelCloser, e // handleLocalCloseReq kicks-off the workflow to execute a cooperative or // forced unilateral closure of the channel initiated by a local subsystem. -// -// TODO(roasbeef): if no more active channels with peer call Remove on connMgr -// with peerID func (p *peer) handleLocalCloseReq(req *htlcswitch.ChanClose) { chanID := lnwire.NewChanIDFromOutPoint(req.ChanPoint) @@ -1847,6 +1844,18 @@ func (p *peer) finalizeChanClosure(chanCloser *channelCloser) { }, } } + + // Remove the persistent connection to this peer if we + // no longer have open channels with them. + p.activeChanMtx.Lock() + numActiveChans := len(p.activeChannels) + p.activeChanMtx.Unlock() + + if numActiveChans == 0 { + p.server.prunePersistentPeerConnection( + p.pubKeyBytes, + ) + } }) } @@ -1900,6 +1909,9 @@ func (p *peer) WipeChannel(chanPoint *wire.OutPoint) error { if channel, ok := p.activeChannels[chanID]; ok { channel.Stop() delete(p.activeChannels, chanID) + if len(p.activeChannels) == 0 { + p.server.prunePersistentPeerConnection(p.pubKeyBytes) + } } p.activeChanMtx.Unlock() diff --git a/server.go b/server.go index d8ed46ca1..9234cb4e1 100644 --- a/server.go +++ b/server.go @@ -1476,6 +1476,22 @@ func (s *server) establishPersistentConnections() error { return nil } +// prunePersistentPeerConnection removes all internal state related to +// persistent connections to a peer within the server. This is used to avoid +// persistent connection retries to peers we do not have any open channels with. +func (s *server) prunePersistentPeerConnection(compressedPubKey [33]byte) { + srvrLog.Infof("Pruning peer %x from persistent connections, number of "+ + "open channels is now zero", compressedPubKey) + + pubKeyStr := string(compressedPubKey[:]) + + s.mu.Lock() + delete(s.persistentPeers, pubKeyStr) + delete(s.persistentPeersBackoff, pubKeyStr) + s.cancelConnReqs(pubKeyStr, nil) + s.mu.Unlock() +} + // BroadcastMessage sends a request to the server to broadcast a set of // messages to all peers other than the one specified by the `skips` parameter. //