From 70611007e5cbe410794edde1f65c2e04928f6e9a Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 23 Jan 2019 07:43:12 -0800 Subject: [PATCH] avoid spawning goroutines for canceled dials This may speed up working through our dial queue a bit (bit probably isn't the main issue). --- p2p/net/swarm/limiter.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/p2p/net/swarm/limiter.go b/p2p/net/swarm/limiter.go index 02aed50a2e..11c20bc100 100644 --- a/p2p/net/swarm/limiter.go +++ b/p2p/net/swarm/limiter.go @@ -77,17 +77,26 @@ func newDialLimiterWithParams(df dialfunc, fdLimit, perPeerLimit int) *dialLimit func (dl *dialLimiter) freeFDToken() { dl.fdConsuming-- - if len(dl.waitingOnFd) > 0 { + for len(dl.waitingOnFd) > 0 { next := dl.waitingOnFd[0] dl.waitingOnFd[0] = nil // clear out memory dl.waitingOnFd = dl.waitingOnFd[1:] + if len(dl.waitingOnFd) == 0 { - dl.waitingOnFd = nil // clear out memory + // clear out memory. + dl.waitingOnFd = nil + } + + // Skip over canceled dials instead of queuing up a goroutine. + if next.cancelled() { + dl.freePeerToken(next) + continue } dl.fdConsuming++ // we already have activePerPeer token at this point so we can just dial go dl.executeDial(next) + return } } @@ -99,18 +108,25 @@ func (dl *dialLimiter) freePeerToken(dj *dialJob) { } waitlist := dl.waitingOnPeerLimit[dj.peer] - if len(waitlist) > 0 { + for len(waitlist) > 0 { next := waitlist[0] - if len(waitlist) == 1 { + waitlist[0] = nil // clear out memory + waitlist = waitlist[1:] + + if len(waitlist) == 0 { delete(dl.waitingOnPeerLimit, next.peer) } else { - waitlist[0] = nil // clear out memory - dl.waitingOnPeerLimit[next.peer] = waitlist[1:] + dl.waitingOnPeerLimit[next.peer] = waitlist + } + + if next.cancelled() { + continue } dl.activePerPeer[next.peer]++ // just kidding, we still want this token dl.addCheckFdLimit(next) + return } }