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

Commit

Permalink
don't count connections in the grace period against the limit
Browse files Browse the repository at this point in the history
fixes #46
  • Loading branch information
Stebalien committed Jul 25, 2019
1 parent 454e88e commit 4c6ee4b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn {

npeers := cm.segments.countPeers()
candidates := make([]*peerInfo, 0, npeers)
ncandidates := 0
cm.plk.RLock()
for _, s := range cm.segments {
s.Lock()
Expand All @@ -228,12 +229,25 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn {
// skip over protected peer.
continue
}
if inf.firstSeen.Add(cm.gracePeriod).After(now) {
// skip peers in the grace period.
continue
}
candidates = append(candidates, inf)
ncandidates += len(inf.conns)
}
s.Unlock()
}
cm.plk.RUnlock()

if ncandidates < cm.lowWater {
// We have too many connections but fewer than lowWater
// connections out of the grace period.
//
// If we trimmed now, we'd kill potentially useful connections.
return nil
}

// Sort peers according to their value.
sort.Slice(candidates, func(i, j int) bool {
left, right := candidates[i], candidates[j]
Expand All @@ -245,7 +259,7 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn {
return left.value < right.value
})

target := nconns - cm.lowWater
target := ncandidates - cm.lowWater

// slightly overallocate because we may have more than one conns per peer
selected := make([]inet.Conn, 0, target+10)
Expand Down

0 comments on commit 4c6ee4b

Please sign in to comment.