diff --git a/config/config.go b/config/config.go index b2cf297566..6e9ce805b8 100644 --- a/config/config.go +++ b/config/config.go @@ -75,6 +75,8 @@ type Config struct { Insecure bool PSK pnet.PSK + DialTimeout time.Duration + RelayCustom bool Relay bool // should the relay transport be used @@ -136,8 +138,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) error { diff --git a/options.go b/options.go index 915938b857..079687746f 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 + } +}