Skip to content

Commit

Permalink
add an option for the swarm dial timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Dec 19, 2021
1 parent 9bb973c commit bf46ccd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
14 changes: 13 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
10 changes: 10 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit bf46ccd

Please sign in to comment.