Skip to content

Commit

Permalink
Merge pull request #882 from libp2p/feat/throttleOption
Browse files Browse the repository at this point in the history
Expose option for setting autonat throttling
  • Loading branch information
Stebalien committed Apr 13, 2020
2 parents fee77aa + c08993b commit a0f66ec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
34 changes: 24 additions & 10 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 @@ -45,7 +46,14 @@ type NATManagerC func(network.Network) bhost.NATManager

type RoutingC func(host.Host) (routing.PeerRouting, error)

// AutoNATMode defines the AutoNAT behavior for the libp2p host.
// autoNATConfig defines the AutoNAT behavior for the libp2p host.
type AutoNATConfig struct {
ForceReachability *network.Reachability
EnableService bool
ThrottleGlobalLimit int
ThrottlePeerLimit int
ThrottleInterval time.Duration
}

// Config describes a set of settings for a libp2p node
//
Expand Down Expand Up @@ -84,9 +92,8 @@ type Config struct {
Routing RoutingC

EnableAutoRelay bool
Reachability network.Reachability
AutoNATService bool
StaticRelays []peer.AddrInfo
AutoNATConfig
StaticRelays []peer.AddrInfo
}

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

autonatOpts := []autonat.Option{autonat.UsingAddresses(func() []ma.Multiaddr {
return addrF(h.AllAddrs())
})}
if cfg.AutoNATService {
autonatOpts := []autonat.Option{
autonat.UsingAddresses(func() []ma.Multiaddr {
return addrF(h.AllAddrs())
}),
}
if cfg.AutoNATConfig.ThrottleInterval != 0 {
autonatOpts = append(autonatOpts,
autonat.WithThrottling(cfg.AutoNATConfig.ThrottleGlobalLimit, cfg.AutoNATConfig.ThrottleInterval),
autonat.WithPeerThrottling(cfg.AutoNATConfig.ThrottlePeerLimit))
}
if cfg.AutoNATConfig.EnableService {
autonatPrivKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
return nil, err
Expand Down Expand Up @@ -322,8 +336,8 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
// closed (as long as we close the underlying network).
autonatOpts = append(autonatOpts, autonat.EnableService(dialerHost.Network()))
}
if cfg.Reachability != network.ReachabilityUnknown {
autonatOpts = append(autonatOpts, autonat.WithReachability(cfg.Reachability))
if cfg.AutoNATConfig.ForceReachability != nil {
autonatOpts = append(autonatOpts, autonat.WithReachability(*cfg.AutoNATConfig.ForceReachability))
}

if _, err = autonat.New(ctx, h, autonatOpts...); err != nil {
Expand Down
22 changes: 19 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package libp2p
import (
"fmt"
"net"
"time"

"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
Expand Down Expand Up @@ -283,7 +284,8 @@ func DefaultStaticRelays() Option {
// forcing the local node to believe it is reachable externally.
func ForceReachabilityPublic() Option {
return func(cfg *Config) error {
cfg.Reachability = network.ReachabilityPublic
public := network.Reachability(network.ReachabilityPublic)
cfg.AutoNATConfig.ForceReachability = &public
return nil
}
}
Expand All @@ -292,7 +294,8 @@ func ForceReachabilityPublic() Option {
// forceing the local node to believe it is behind a NAT and not reachable externally.
func ForceReachabilityPrivate() Option {
return func(cfg *Config) error {
cfg.Reachability = network.ReachabilityPrivate
private := network.Reachability(network.ReachabilityPrivate)
cfg.AutoNATConfig.ForceReachability = &private
return nil
}
}
Expand All @@ -302,7 +305,20 @@ func ForceReachabilityPrivate() Option {
// to peers, and then tell them if it was successful in making such connections.
func EnableNATService() Option {
return func(cfg *Config) error {
cfg.AutoNATService = true
cfg.AutoNATConfig.EnableService = true
return nil
}
}

// AutoNATServiceRateLimit 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 AutoNATServiceRateLimit(global, perPeer int, interval time.Duration) Option {
return func(cfg *Config) error {
cfg.AutoNATConfig.ThrottleGlobalLimit = global
cfg.AutoNATConfig.ThrottlePeerLimit = perPeer
cfg.AutoNATConfig.ThrottleInterval = interval
return nil
}
}
Expand Down

0 comments on commit a0f66ec

Please sign in to comment.