mirror of
https://github.com/libp2p/go-libp2p.git
synced 2026-04-22 16:17:19 +08:00
add an option for the swarm dial timeout
This commit is contained in:
+13
-1
@@ -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
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user