Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
don't set linger to 0
Browse files Browse the repository at this point in the history
We should do this from
go-tcp-transport (libp2p/go-tcp-transport#36), not here,
as setting linger to 0 will cause us to send RST packets instead of nicely
closing connections.

Also, document why we need the fallback dialer.
  • Loading branch information
Stebalien committed Apr 4, 2019
1 parent 501048f commit 49f05fa
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions reuseport.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func reuseErrShouldRetry(err error) bool {
}

// Dials using reuseport and then redials normally if that fails.
// TODO(anacrolix): This shouldn't fail anymore: Remove fallback.
func reuseDial(ctx context.Context, laddr *net.TCPAddr, network, raddr string) (con net.Conn, err error) {
if laddr == nil {
return fallbackDialer.DialContext(ctx, network, raddr)
Expand All @@ -49,22 +48,15 @@ func reuseDial(ctx context.Context, laddr *net.TCPAddr, network, raddr string) (
LocalAddr: laddr,
Control: reuseport.Control,
}
defer func() {
if err != nil {
return
}
// This is transplanted from go-reuseport, which once set no linger on
// dialing and may be a requirement for desired behaviour in this
// package.
con.(*net.TCPConn).SetLinger(0)
}()

con, err = d.DialContext(ctx, network, raddr)
if err == nil {
return con, nil
}

if reuseErrShouldRetry(err) && ctx.Err() == nil {
// We could have an existing socket open or we could have one
// stuck in TIME-WAIT.
log.Debugf("failed to reuse port, dialing with a random port: %s", err)
con, err = fallbackDialer.DialContext(ctx, network, raddr)
}
Expand Down

0 comments on commit 49f05fa

Please sign in to comment.