Skip to content

Commit

Permalink
always trim to the low watermark in a memory emergency
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Dec 13, 2021
1 parent 3412150 commit ff0577e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions p2p/net/connmgr/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,18 @@ func NewConnManager(low, hi int, opts ...Option) (*BasicConnMgr, error) {
}

// memoryEmergency is run when we run low on memory.
// Close half of the connections, as quickly as possible.
// Close connections until we right the low watermark.
// We don't pay attention to the silence period or the grace period.
// We try to not kill protected connections, but if that turns out to be necessary, not connection is safe!
func (cm *BasicConnMgr) memoryEmergency() {
log.Info("Low on memory. Closing half of our connections.")
target := int(atomic.LoadInt32(&cm.connCount) / 2)
connCount := int(atomic.LoadInt32(&cm.connCount))
target := connCount - cm.cfg.lowWater
if target < 0 {
log.Warnw("Low on memory, but we only have a few connections", "num", connCount, "low watermark", cm.cfg.lowWater)
return
} else {
log.Warnf("Low on memory. Closing %d connections.", target)
}

cm.trimMutex.Lock()
defer atomic.AddUint64(&cm.trimCount, 1)
Expand Down

0 comments on commit ff0577e

Please sign in to comment.