Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): remove disconnected peers from peerMgr #1110

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions network/peermgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@
mgr.lk.Lock()
defer mgr.lk.Unlock()

mgr.addPeer(pid, ma, direction)
}

func (mgr *peerMgr) addPeer(pid lp2ppeer.ID, ma multiaddr.Multiaddr,
direction lp2pnet.Direction,
) {
_, exists := mgr.peers[pid]
if exists {
return
}

switch direction {
case lp2pnet.DirInbound:
mgr.numInbound++
Expand All @@ -96,14 +107,18 @@
mgr.lk.Lock()
defer mgr.lk.Unlock()

peer, ok := mgr.peers[pid]
if !ok {
mgr.removePeer(pid)
}

func (mgr *peerMgr) removePeer(pid lp2ppeer.ID) {
peerInfo, exists := mgr.peers[pid]
if !exists {
mgr.logger.Warn("unable to find a peer", "pid", pid)

return
}

switch peer.Direction {
switch peerInfo.Direction {
case lp2pnet.DirInbound:
mgr.numInbound--

Expand Down Expand Up @@ -132,18 +147,30 @@
mgr.lk.Lock()
defer mgr.lk.Unlock()

connectedPeers := len(mgr.peers)
net := mgr.host.Network()

// Let's check if some peers are disconnected
var connectedPeers []lp2ppeer.ID
for pid := range mgr.peers {
connectedness := net.Connectedness(pid)
if connectedness == lp2pnet.Connected {
connectedPeers = append(connectedPeers, pid)
} else {
mgr.removePeer(pid)

Check warning on line 159 in network/peermgr.go

View check run for this annotation

Codecov / codecov/patch

network/peermgr.go#L159

Added line #L159 was not covered by tests
}
}

mgr.logger.Debug("check connectivity", "peers", connectedPeers)

switch {
case connectedPeers > mgr.maxConns:
case len(connectedPeers) > mgr.maxConns:

Check warning on line 166 in network/peermgr.go

View check run for this annotation

Codecov / codecov/patch

network/peermgr.go#L166

Added line #L166 was not covered by tests
mgr.logger.Debug("peer count is about maximum threshold",
"count", connectedPeers,
"max", mgr.maxConns)

return

case connectedPeers < mgr.minConns:
case len(connectedPeers) < mgr.minConns:
mgr.logger.Info("peer count is less than minimum threshold",
"count", connectedPeers,
"min", mgr.minConns)
Expand All @@ -157,7 +184,7 @@
mgr.logger.Debug("try connecting to a bootstrap peer", "peer", ai.String())

// Don't try to connect to an already connected peer.
if mgr.host.Network().Connectedness(ai.ID) == lp2pnet.Connected {
if HasPID(connectedPeers, ai.ID) {
mgr.logger.Trace("already connected", "peer", ai.String())

continue
Expand Down
2 changes: 2 additions & 0 deletions network/peermgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func TestNumInboundOutbound(t *testing.T) {
net.peerMgr.AddPeer(pid1, addr, lp2pnet.DirInbound)
net.peerMgr.AddPeer(pid2, addr, lp2pnet.DirOutbound)
net.peerMgr.AddPeer(pid3, addr, lp2pnet.DirOutbound)
// Adding pid1 again
net.peerMgr.AddPeer(pid1, addr, lp2pnet.DirInbound)

assert.Equal(t, 1, net.peerMgr.NumInbound())
assert.Equal(t, 2, net.peerMgr.NumOutbound())
Expand Down
Loading