Skip to content

Commit 39eec50

Browse files
authored
Merge pull request #92 from libp2p/chore/add-dial-timeout
feat: add a DialTimeout function
2 parents 0ae94e3 + 5b71e44 commit 39eec50

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

interface.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"fmt"
2323
"net"
24+
"time"
2425
)
2526

2627
// Available returns whether or not SO_REUSEPORT or equivalent behaviour is
@@ -47,17 +48,25 @@ func ListenPacket(network, address string) (net.PacketConn, error) {
4748
return listenConfig.ListenPacket(context.Background(), network, address)
4849
}
4950

50-
// Dial dials the given network and address. see net.Dialer.Dial
51+
// Dial dials the given network and address. see net.Dial
5152
// Returns a net.Conn created from a file descriptor for a socket
5253
// with SO_REUSEPORT and SO_REUSEADDR option set.
5354
func Dial(network, laddr, raddr string) (net.Conn, error) {
55+
return DialTimeout(network, laddr, raddr, time.Duration(0))
56+
}
57+
58+
// Dial dials the given network and address, with the given timeout. see
59+
// net.DialTimeout Returns a net.Conn created from a file descriptor for
60+
// a socket with SO_REUSEPORT and SO_REUSEADDR option set.
61+
func DialTimeout(network, laddr, raddr string, timeout time.Duration) (net.Conn, error) {
5462
nla, err := ResolveAddr(network, laddr)
5563
if err != nil {
5664
return nil, fmt.Errorf("failed to resolve local addr: %w", err)
5765
}
5866
d := net.Dialer{
5967
Control: Control,
6068
LocalAddr: nla,
69+
Timeout: timeout,
6170
}
6271
return d.Dial(network, raddr)
6372
}

0 commit comments

Comments
 (0)