add an option for the swarm dial timeout

This commit is contained in:
Marten Seemann
2021-12-19 22:11:54 +04:00
parent 9add9a328d
commit d24f4f20f8
2 changed files with 23 additions and 1 deletions
+13 -1
View File
@@ -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 {
+10
View File
@@ -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
}
}