Skip to content

Commit

Permalink
Merge pull request #101 from libp2p/fix/non-default-emergency-trimming
Browse files Browse the repository at this point in the history
make emergency trimming optional, disabled by default
  • Loading branch information
vyzo authored Jan 14, 2022
2 parents 1a3d6a6 + ee3785b commit b2736df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions p2p/net/connmgr/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ func NewConnManager(low, hi int, opts ...Option) (*BasicConnMgr, error) {
}
cm.ctx, cm.cancel = context.WithCancel(context.Background())

// When we're running low on memory, immediately trigger a trim.
cm.unregisterWatchdog = watchdog.RegisterPostGCNotifee(cm.memoryEmergency)
if cfg.emergencyTrim {
// When we're running low on memory, immediately trigger a trim.
cm.unregisterWatchdog = watchdog.RegisterPostGCNotifee(cm.memoryEmergency)
} else {
cm.unregisterWatchdog = func() {}
}

decay, _ := NewDecayer(cfg.decayer, cm)
cm.decayer = decay
Expand Down
9 changes: 9 additions & 0 deletions p2p/net/connmgr/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type config struct {
gracePeriod time.Duration
silencePeriod time.Duration
decayer *DecayerCfg
emergencyTrim bool
}

// Option represents an option for the basic connection manager.
Expand Down Expand Up @@ -50,3 +51,11 @@ func WithSilencePeriod(p time.Duration) Option {
return nil
}
}

// WithEmergencyTrim is an option to enable trimming connections on memory emergency.
func WithEmergencyTrim(enable bool) Option {
return func(cfg *config) error {
cfg.emergencyTrim = enable
return nil
}
}

0 comments on commit b2736df

Please sign in to comment.