peer+server: remove persistent connections for peers with no open channels remaining

This commit is contained in:
Wilmer Paulino 2018-06-15 12:33:24 -07:00
parent 0e4d350e56
commit 38b52df51f
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F
2 changed files with 31 additions and 3 deletions

18
peer.go
View File

@ -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()

View File

@ -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.
//