From 234c13d26a11e88549c169ce756eef90d5113bf8 Mon Sep 17 00:00:00 2001 From: vyzo Date: Tue, 14 May 2019 15:21:19 +0300 Subject: [PATCH] fix issue #44 --- p2p/net/connmgr/connmgr.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/p2p/net/connmgr/connmgr.go b/p2p/net/connmgr/connmgr.go index 9a3912da5b..e61ad15ca9 100644 --- a/p2p/net/connmgr/connmgr.go +++ b/p2p/net/connmgr/connmgr.go @@ -194,12 +194,13 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn { return nil } now := time.Now() - npeers := cm.segments.countPeers() - if npeers <= cm.lowWater { + nconns := int(atomic.LoadInt32(&cm.connCount)) + if nconns <= cm.lowWater { log.Info("open connection count below limit") return nil } + npeers := cm.segments.countPeers() candidates := make([]*peerInfo, 0, npeers) cm.plk.RLock() for _, s := range cm.segments { @@ -220,12 +221,10 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn { return candidates[i].value < candidates[j].value }) - target := npeers - cm.lowWater + target := nconns - cm.lowWater - // 2x number of peers we're disconnecting from because we may have more - // than one connection per peer. Slightly over allocating isn't an issue - // as this is a very short-lived array. - selected := make([]inet.Conn, 0, target*2) + // slightly overallocate because we may have more than one conns per peer + selected := make([]inet.Conn, 0, target+10) for _, inf := range candidates { // TODO: should we be using firstSeen or the time associated with the connection itself? @@ -239,10 +238,10 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn { for c := range inf.conns { selected = append(selected, c) } + target -= len(inf.conns) s.Unlock() - target-- - if target == 0 { + if target <= 0 { break } }