Skip to content

Commit

Permalink
Merge pull request #5483 from oasisprotocol/peternose/stable/23.0.x/b…
Browse files Browse the repository at this point in the history
…ackport-5480

[BACKPORT/23.0.x] go/p2p/peermgmt: Find peers and connect only when needed
  • Loading branch information
peternose authored Nov 28, 2023
2 parents 57638a6 + 958d977 commit a8927e5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions .cargo/audit.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
ignore = [
"RUSTSEC-2020-0071", # Remove once upstream dependencies are updated.
"RUSTSEC-2021-0124", # Remove once upstream dependencies are updated.
"RUSTSEC-2023-0071", # Does not affect our current use of the library.
]
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.
Empty file added .changelog/5484.trivial.md
Empty file.
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 a8927e5

Please sign in to comment.