status-go/vendor/github.com/libp2p/go-libp2p/p2p/net/connmgr/options.go

65 lines
1.5 KiB
Go
Raw Normal View History

2022-04-01 16:16:46 +00:00
package connmgr
import (
"errors"
"time"
2022-11-04 13:57:20 +00:00
"github.com/benbjohnson/clock"
2022-04-01 16:16:46 +00:00
)
// config is the configuration struct for the basic connection manager.
type config struct {
highWater int
lowWater int
gracePeriod time.Duration
silencePeriod time.Duration
decayer *DecayerCfg
emergencyTrim bool
2022-11-04 13:57:20 +00:00
clock clock.Clock
2022-04-01 16:16:46 +00:00
}
// Option represents an option for the basic connection manager.
type Option func(*config) error
// DecayerConfig applies a configuration for the decayer.
func DecayerConfig(opts *DecayerCfg) Option {
return func(cfg *config) error {
cfg.decayer = opts
return nil
}
}
2022-11-04 13:57:20 +00:00
// WithClock sets the internal clock impl
func WithClock(c clock.Clock) Option {
return func(cfg *config) error {
cfg.clock = c
return nil
}
}
2022-04-01 16:16:46 +00:00
// WithGracePeriod sets the grace period.
// The grace period is the time a newly opened connection is given before it becomes
// subject to pruning.
func WithGracePeriod(p time.Duration) Option {
return func(cfg *config) error {
if p < 0 {
return errors.New("grace period must be non-negative")
}
cfg.gracePeriod = p
return nil
}
}
// WithSilencePeriod sets the silence period.
// The connection manager will perform a cleanup once per silence period
// if the number of connections surpasses the high watermark.
func WithSilencePeriod(p time.Duration) Option {
return func(cfg *config) error {
if p <= 0 {
return errors.New("silence period must be non-zero")
}
cfg.silencePeriod = p
return nil
}
}