Skip to content

Commit c46c576

Browse files
authored
Merge pull request #6 from libp2p/reuseport-control
Update to go-reuseport 0.2.0
2 parents 33bd60a + 8b89db3 commit c46c576

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

p2p/net/reuseport/reuseport.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,28 @@ func reuseErrShouldRetry(err error) bool {
3838
}
3939
}
4040

41-
// Dials using reusport and then redials normally if that fails.
42-
func reuseDial(ctx context.Context, laddr *net.TCPAddr, network, raddr string) (net.Conn, error) {
41+
// Dials using reuseport and then redials normally if that fails.
42+
// TODO(anacrolix): This shouldn't fail anymore: Remove fallback.
43+
func reuseDial(ctx context.Context, laddr *net.TCPAddr, network, raddr string) (con net.Conn, err error) {
4344
if laddr == nil {
4445
return fallbackDialer.DialContext(ctx, network, raddr)
4546
}
4647

47-
d := reuseport.Dialer{
48-
D: net.Dialer{
49-
LocalAddr: laddr,
50-
},
48+
d := net.Dialer{
49+
LocalAddr: laddr,
50+
Control: reuseport.Control,
5151
}
52+
defer func() {
53+
if err != nil {
54+
return
55+
}
56+
// This is transplanted from go-reuseport, which once set no linger on
57+
// dialing and may be a requirement for desired behaviour in this
58+
// package.
59+
con.(*net.TCPConn).SetLinger(0)
60+
}()
5261

53-
con, err := d.DialContext(ctx, network, raddr)
62+
con, err = d.DialContext(ctx, network, raddr)
5463
if err == nil {
5564
return con, nil
5665
}

0 commit comments

Comments
 (0)