From 28d323995891080484617c076649b63a504c5bf4 Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Mon, 20 Jun 2022 11:47:56 -0400 Subject: [PATCH] p2p: wake dialing thread after sleep (#8803) --- internal/p2p/peermanager.go | 9 +++++++++ internal/p2p/router.go | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/internal/p2p/peermanager.go b/internal/p2p/peermanager.go index a5438b9ec..210c34e2a 100644 --- a/internal/p2p/peermanager.go +++ b/internal/p2p/peermanager.go @@ -498,6 +498,15 @@ func (m *PeerManager) HasMaxPeerCapacity() bool { return len(m.connected) >= int(m.options.MaxConnected) } +func (m *PeerManager) HasDialedMaxPeers() bool { + m.mtx.Lock() + defer m.mtx.Unlock() + + stats := m.getConnectedInfo() + + return stats.outgoing >= m.options.MaxOutgoingConnections +} + // DialNext finds an appropriate peer address to dial, and marks it as dialing. // If no peer is found, or all connection slots are full, it blocks until one // becomes available. The caller must call Dialed() or DialFailed() for the diff --git a/internal/p2p/router.go b/internal/p2p/router.go index 55dc73720..ec225f8e3 100644 --- a/internal/p2p/router.go +++ b/internal/p2p/router.go @@ -466,6 +466,11 @@ func (r *Router) dialSleep(ctx context.Context) { } r.options.DialSleep(ctx) + + if !r.peerManager.HasDialedMaxPeers() { + r.peerManager.dialWaker.Wake() + } + } // acceptPeers accepts inbound connections from peers on the given transport,