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

Expose option for setting autonat throttling #882

Merged
merged 3 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"fmt"
"time"

"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
Expand Down Expand Up @@ -83,10 +84,12 @@ type Config struct {

Routing RoutingC

EnableAutoRelay bool
Reachability network.Reachability
AutoNATService bool
StaticRelays []peer.AddrInfo
EnableAutoRelay bool
Reachability network.Reachability
AutoNATService bool
AutoNATThrottling bool
AutoNATThrottles [2]int
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
StaticRelays []peer.AddrInfo
}

func (cfg *Config) makeSwarm(ctx context.Context) (*swarm.Swarm, error) {
Expand Down Expand Up @@ -280,9 +283,16 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
}
}

autonatOpts := []autonat.Option{autonat.UsingAddresses(func() []ma.Multiaddr {
return addrF(h.AllAddrs())
})}
autonatOpts := []autonat.Option{
autonat.UsingAddresses(func() []ma.Multiaddr {
return addrF(h.AllAddrs())
}),
}
if cfg.AutoNATThrottling {
autonatOpts = append(autonatOpts,
autonat.WithThrottling(cfg.AutoNATThrottles[0], 1*time.Minute),
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
autonat.WithPeerThrottling(cfg.AutoNATThrottles[1]))
}
if cfg.AutoNATService {
autonatPrivKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,19 @@ func EnableNATService() Option {
}
}

// OverrideNATServiceThrottling changes the default rate limiting configured in helping
// other peers determine their reachability status. When set, the host will limit
// the number of requests it responds to in each 60 second period to the set
// numbers. A value of '0' disables throttling.
func OverrideNATServiceThrottling(globalThrottle, perPeerThrottle int) Option {
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
return func(cfg *Config) error {
cfg.AutoNATThrottling = true
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
cfg.AutoNATThrottles[0] = globalThrottle
cfg.AutoNATThrottles[1] = perPeerThrottle
return nil
}
}

// FilterAddresses configures libp2p to never dial nor accept connections from
// the given addresses. FilterAddresses should be used for cases where the
// addresses you want to deny are known ahead of time.
Expand Down