From 9d6439bbe69a617b21ea9509246cf1b757b856ad Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 26 Mar 2021 12:26:45 +0300 Subject: [PATCH] network: don't attempt to connect to the same node twice We can have multiple copies of the same address in the pool and we should only proceed to connect once per attempt. --- pkg/network/discovery.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/network/discovery.go b/pkg/network/discovery.go index bdf391e39..21704f452 100644 --- a/pkg/network/discovery.go +++ b/pkg/network/discovery.go @@ -37,6 +37,7 @@ type DefaultDiscovery struct { connectedAddrs map[string]bool goodAddrs map[string]bool unconnectedAddrs map[string]int + attempted map[string]bool isDead bool requestCh chan int pool chan string @@ -52,6 +53,7 @@ func NewDefaultDiscovery(addrs []string, dt time.Duration, ts Transporter) *Defa connectedAddrs: make(map[string]bool), goodAddrs: make(map[string]bool), unconnectedAddrs: make(map[string]int), + attempted: make(map[string]bool), requestCh: make(chan int), pool: make(chan string, maxPoolSize), } @@ -174,7 +176,11 @@ func (d *DefaultDiscovery) RegisterConnectedAddr(addr string) { } func (d *DefaultDiscovery) tryAddress(addr string) { - if err := d.transport.Dial(addr, d.dialTimeout); err != nil { + err := d.transport.Dial(addr, d.dialTimeout) + d.lock.Lock() + delete(d.attempted, addr) + d.lock.Unlock() + if err != nil { d.RegisterBadAddr(addr) d.RequestRemote(1) } @@ -210,14 +216,14 @@ func (d *DefaultDiscovery) run() { requested = r } case addr := <-d.pool: - d.lock.RLock() - addrIsConnected := d.connectedAddrs[addr] - d.lock.RUnlock() updatePoolCountMetric(d.PoolCount()) - if !addrIsConnected { + d.lock.Lock() + if !d.connectedAddrs[addr] && !d.attempted[addr] { + d.attempted[addr] = true go d.tryAddress(addr) requested-- } + d.lock.Unlock() default: // Empty pool d.lock.Lock() for _, addr := range d.seeds {