Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - p2p: disable autoscaling and drop from bootnodes on enough peers #4770

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions p2p/dhtdiscovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
}
}

func WithHighPeers(peers int) Opt {
return func(d *Discovery) {
d.highPeers = peers
}
}

func WithBootnodes(bootnodes []peer.AddrInfo) Opt {
return func(d *Discovery) {
d.bootnodes = bootnodes
Expand Down Expand Up @@ -90,6 +96,7 @@
timeout: 30 * time.Second,
bootstrapDuration: 30 * time.Second,
minPeers: 20,
highPeers: 40,
}
for _, opt := range opts {
opt(&d)
Expand Down Expand Up @@ -121,10 +128,10 @@
// how often to check if we have enough peers
period time.Duration
// timeout used for connections
timeout time.Duration
bootstrapDuration time.Duration
minPeers int
backup, bootnodes []peer.AddrInfo
timeout time.Duration
bootstrapDuration time.Duration
minPeers, highPeers int
backup, bootnodes []peer.AddrInfo
}

func (d *Discovery) Start() {
Expand Down Expand Up @@ -157,6 +164,14 @@
zap.Int("required", d.minPeers),
zap.Int("connected", connected),
)
if connected >= d.highPeers {
for _, boot := range d.bootnodes {
if err := d.h.Network().ClosePeer(boot.ID); err != nil {
d.logger.Warn("failed to close bootnode connection",
zap.Stringer("address", boot), zap.Error(err))
}

Check warning on line 172 in p2p/dhtdiscovery/discovery.go

View check run for this annotation

Codecov / codecov/patch

p2p/dhtdiscovery/discovery.go#L168-L172

Added lines #L168 - L172 were not covered by tests
}
}
} else {
d.connect(&connEg, d.backup)
// no reason to spend more resources if we got enough from backup
Expand Down
16 changes: 12 additions & 4 deletions p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
MinPeers int `mapstructure:"min-peers"`
LowPeers int `mapstructure:"low-peers"`
HighPeers int `mapstructure:"high-peers"`
AutoscalePeers bool `mapstructure:"autoscale-peers"`
AdvertiseAddress string `mapstructure:"advertise-address"`
AcceptQueue int `mapstructure:"p2p-accept-queue"`
Metrics bool `mapstructure:"p2p-metrics"`
Expand Down Expand Up @@ -179,7 +180,7 @@
lopts = append(lopts, libp2p.ForceReachabilityPrivate())
}
if cfg.Metrics {
lopts = append(lopts, setupResourcesManager(cfg.HighPeers))
lopts = append(lopts, setupResourcesManager(cfg))

Check warning on line 183 in p2p/host.go

View check run for this annotation

Codecov / codecov/patch

p2p/host.go#L183

Added line #L183 was not covered by tests
}
if !cfg.DisableNatPort {
lopts = append(lopts, libp2p.NATPortMap())
Expand All @@ -200,17 +201,18 @@
return Upgrade(h, opts...)
}

func setupResourcesManager(highPeers int) func(cfg *libp2p.Config) error {
func setupResourcesManager(hostcfg Config) func(cfg *libp2p.Config) error {

Check warning on line 204 in p2p/host.go

View check run for this annotation

Codecov / codecov/patch

p2p/host.go#L204

Added line #L204 was not covered by tests
return func(cfg *libp2p.Config) error {
rcmgrObs.MustRegisterWith(prometheus.DefaultRegisterer)
str, err := rcmgrObs.NewStatsTraceReporter()
if err != nil {
return err
}
highPeers := hostcfg.HighPeers

Check warning on line 211 in p2p/host.go

View check run for this annotation

Codecov / codecov/patch

p2p/host.go#L211

Added line #L211 was not covered by tests
limits := rcmgr.DefaultLimits
limits.SystemBaseLimit.ConnsInbound = highPeers
limits.SystemBaseLimit.ConnsOutbound = highPeers
limits.SystemBaseLimit.Conns = 2 * highPeers
limits.SystemBaseLimit.Conns = highPeers + hostcfg.MinPeers

Check warning on line 215 in p2p/host.go

View check run for this annotation

Codecov / codecov/patch

p2p/host.go#L215

Added line #L215 was not covered by tests
limits.SystemBaseLimit.FD = 2 * highPeers
limits.SystemBaseLimit.StreamsInbound = 8 * highPeers
limits.SystemBaseLimit.StreamsOutbound = 8 * highPeers
Expand All @@ -219,13 +221,19 @@
limits.ServiceBaseLimit.StreamsOutbound = 8 * highPeers
limits.ServiceBaseLimit.Streams = 16 * highPeers

limits.TransientBaseLimit.Conns = hostcfg.MinPeers

Check warning on line 225 in p2p/host.go

View check run for this annotation

Codecov / codecov/patch

p2p/host.go#L224-L225

Added lines #L224 - L225 were not covered by tests
limits.ProtocolBaseLimit.StreamsInbound = 8 * highPeers
limits.ProtocolBaseLimit.StreamsOutbound = 8 * highPeers
limits.ProtocolBaseLimit.Streams = 16 * highPeers
libp2p.SetDefaultServiceLimits(&limits)

concrete := limits.AutoScale()
if !hostcfg.AutoscalePeers {
concrete = limits.Scale(0, 0)
}

Check warning on line 234 in p2p/host.go

View check run for this annotation

Codecov / codecov/patch

p2p/host.go#L231-L234

Added lines #L231 - L234 were not covered by tests
mgr, err := rcmgr.NewResourceManager(
rcmgr.NewFixedLimiter(limits.AutoScale()),
rcmgr.NewFixedLimiter(concrete),

Check warning on line 236 in p2p/host.go

View check run for this annotation

Codecov / codecov/patch

p2p/host.go#L236

Added line #L236 was not covered by tests
rcmgr.WithTraceReporter(str),
)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions p2p/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func Upgrade(h host.Host, opts ...Opt) (*Host, error) {
}

dopts := []discovery.Opt{
discovery.WithMinPeers(cfg.MinPeers),
discovery.WithHighPeers(cfg.HighPeers),
discovery.WithDir(cfg.DataDir),
discovery.WithBootnodes(bootnodes),
discovery.WithLogger(fh.logger.Zap()),
Expand Down