Skip to content

Commit

Permalink
Merge pull request #5480 from oasisprotocol/peternose/bugfix/fetch-pe…
Browse files Browse the repository at this point in the history
…ers-when-needed

go/p2p/peermgmt: Find peers and connect only when needed
  • Loading branch information
peternose authored Nov 28, 2023
2 parents bdbd1cb + 1980088 commit 5da85f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changelog/5480.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go/p2p/peermgmt: Find peers and connect only when needed

If we are already connected to a sufficient number of peers
for a given topic or protocol, there's no need to retrieve
additional peers from the registry or the seed node.
33 changes: 16 additions & 17 deletions go/p2p/peermgmt/peermgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,6 @@ func (m *PeerManager) connectPeers(ctx context.Context, registered bool) {
}()
}

var (
peerCh <-chan peer.AddrInfo
limit int
)

m.mu.Lock()
defer m.mu.Unlock()

Expand All @@ -379,30 +374,34 @@ func (m *PeerManager) connectPeers(ctx context.Context, registered bool) {

switch registered {
case true:
peerCh = m.registry.findProtocolPeers(ctx, p)
limit = d.min - connected
if limit := d.min - connected; limit > 0 {
peerCh := m.registry.findProtocolPeers(ctx, p)
connectPeers(peerCh, limit)
}
default:
peerCh = m.discovery.findPeers(ctx, string(p))
limit = d.total - connected
if limit := d.total - connected; limit > 0 {
peerCh := m.discovery.findPeers(ctx, string(p))
connectPeers(peerCh, limit)
}
}

connectPeers(peerCh, limit)
}

for t, d := range m.topics {
connected := m.NumTopicPeers(t)

switch registered {
case true:
peerCh = m.registry.findTopicPeers(ctx, t)
limit = d.min - connected
if limit := d.min - connected; limit > 0 {
peerCh := m.registry.findTopicPeers(ctx, t)
connectPeers(peerCh, limit)
}

default:
peerCh = m.discovery.findPeers(ctx, t)
limit = d.total - connected
if limit := d.total - connected; limit > 0 {
peerCh := m.discovery.findPeers(ctx, t)
connectPeers(peerCh, limit)
}
}

connectPeers(peerCh, limit)
}
}

Expand Down

0 comments on commit 5da85f3

Please sign in to comment.