switch bool to enum

This commit is contained in:
Will Scott
2020-04-13 10:23:22 -07:00
parent 5473f0ea0e
commit c08993b4e8
2 changed files with 12 additions and 14 deletions
+7 -10
View File
@@ -48,12 +48,11 @@ type RoutingC func(host.Host) (routing.PeerRouting, error)
// autoNATConfig defines the AutoNAT behavior for the libp2p host.
type AutoNATConfig struct {
ForceReachabilityPublic bool
ForceReachabilityPrivate bool
EnableService bool
ThrottleGlobalLimit int
ThrottlePeerLimit int
ThrottleInterval time.Duration
ForceReachability *network.Reachability
EnableService bool
ThrottleGlobalLimit int
ThrottlePeerLimit int
ThrottleInterval time.Duration
}
// Config describes a set of settings for a libp2p node
@@ -337,10 +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.AutoNATConfig.ForceReachabilityPublic {
autonatOpts = append(autonatOpts, autonat.WithReachability(network.ReachabilityPublic))
} else if cfg.AutoNATConfig.ForceReachabilityPrivate {
autonatOpts = append(autonatOpts, autonat.WithReachability(network.ReachabilityPrivate))
if cfg.AutoNATConfig.ForceReachability != nil {
autonatOpts = append(autonatOpts, autonat.WithReachability(*cfg.AutoNATConfig.ForceReachability))
}
if _, err = autonat.New(ctx, h, autonatOpts...); err != nil {
+5 -4
View File
@@ -11,6 +11,7 @@ import (
"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/pnet"
@@ -283,8 +284,8 @@ func DefaultStaticRelays() Option {
// forcing the local node to believe it is reachable externally.
func ForceReachabilityPublic() Option {
return func(cfg *Config) error {
cfg.AutoNATConfig.ForceReachabilityPublic = true
cfg.AutoNATConfig.ForceReachabilityPrivate = false
public := network.Reachability(network.ReachabilityPublic)
cfg.AutoNATConfig.ForceReachability = &public
return nil
}
}
@@ -293,8 +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.AutoNATConfig.ForceReachabilityPrivate = true
cfg.AutoNATConfig.ForceReachabilityPublic = false
private := network.Reachability(network.ReachabilityPrivate)
cfg.AutoNATConfig.ForceReachability = &private
return nil
}
}