Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
fix issue #44
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed May 14, 2019
1 parent 477e851 commit 1e96efe
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,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 {
Expand All @@ -221,12 +222,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?
Expand All @@ -240,10 +239,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
}
}
Expand Down

0 comments on commit 1e96efe

Please sign in to comment.