diff --git a/config/config.go b/config/config.go index 47459b8b27..651e37b58c 100644 --- a/config/config.go +++ b/config/config.go @@ -74,6 +74,8 @@ type Config struct { Insecure bool PSK pnet.PSK + DialTimeout time.Duration + RelayCustom bool Relay bool // should the relay transport be used @@ -135,8 +137,18 @@ func (cfg *Config) makeSwarm() (*swarm.Swarm, error) { return nil, err } + opts := make([]swarm.Option, 0, 3) + if cfg.Reporter != nil { + opts = append(opts, swarm.WithMetrics(cfg.Reporter)) + } + if cfg.ConnectionGater != nil { + opts = append(opts, swarm.WithConnectionGater(cfg.ConnectionGater)) + } + if cfg.DialTimeout != 0 { + opts = append(opts, swarm.WithDialTimeout(cfg.DialTimeout)) + } // TODO: Make the swarm implementation configurable. - return swarm.NewSwarm(pid, cfg.Peerstore, swarm.WithMetrics(cfg.Reporter), swarm.WithConnectionGater(cfg.ConnectionGater)) + return swarm.NewSwarm(pid, cfg.Peerstore, opts...) } func (cfg *Config) addTransports(h host.Host) (err error) { diff --git a/go.mod b/go.mod index 0720c2fe38..5d970c3600 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,7 @@ require ( github.com/libp2p/go-libp2p-noise v0.3.0 github.com/libp2p/go-libp2p-peerstore v0.6.0 github.com/libp2p/go-libp2p-quic-transport v0.15.2 - github.com/libp2p/go-libp2p-swarm v0.9.0 + github.com/libp2p/go-libp2p-swarm v0.9.1-0.20211219154103-1de208c84b89 github.com/libp2p/go-libp2p-testing v0.6.0 github.com/libp2p/go-libp2p-tls v0.3.1 github.com/libp2p/go-libp2p-transport-upgrader v0.6.0 diff --git a/go.sum b/go.sum index ffe215fdd1..703b366a3d 100644 --- a/go.sum +++ b/go.sum @@ -458,8 +458,8 @@ github.com/libp2p/go-libp2p-quic-transport v0.15.2 h1:wHBEceRy+1/8Ec8dAIyr+/P7L2 github.com/libp2p/go-libp2p-quic-transport v0.15.2/go.mod h1:wv4uGwjcqe8Mhjj7N/Ic0aKjA+/10UnMlSzLO0yRpYQ= github.com/libp2p/go-libp2p-swarm v0.3.0/go.mod h1:hdv95GWCTmzkgeJpP+GK/9D9puJegb7H57B5hWQR5Kk= github.com/libp2p/go-libp2p-swarm v0.8.0/go.mod h1:sOMp6dPuqco0r0GHTzfVheVBh6UEL0L1lXUZ5ot2Fvc= -github.com/libp2p/go-libp2p-swarm v0.9.0 h1:LdWjHDVjPMYt3NCG2EHcQiIP8XzA8BHhHz8ZLAYol2Y= -github.com/libp2p/go-libp2p-swarm v0.9.0/go.mod h1:2f8d8uxTJmpeqHF/1ujjdXZp+98nNIbujVOMEZxCbZ8= +github.com/libp2p/go-libp2p-swarm v0.9.1-0.20211219154103-1de208c84b89 h1:ta6pS0U7c74sgpxvghR04DiielJrvmRzqOTJ7GRyXoI= +github.com/libp2p/go-libp2p-swarm v0.9.1-0.20211219154103-1de208c84b89/go.mod h1:2f8d8uxTJmpeqHF/1ujjdXZp+98nNIbujVOMEZxCbZ8= github.com/libp2p/go-libp2p-testing v0.0.3/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E= github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0= github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod h1:Qy8sAncLKpwXtS2dSnDOP8ktexIAHKu+J+pnZOFZLTc= diff --git a/options.go b/options.go index 863d519597..9f1eb3d0db 100644 --- a/options.go +++ b/options.go @@ -461,3 +461,13 @@ func EnableHolePunching(opts ...holepunch.Option) Option { return nil } } + +func WithDialTimeout(t time.Duration) Option { + return func(cfg *Config) error { + if t <= 0 { + return errors.New("dial timeout needs to be non-negative") + } + cfg.DialTimeout = t + return nil + } +}