2018-07-04 10:51:47 +00:00
|
|
|
package tcpreuse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
reuseport "github.com/libp2p/go-reuseport"
|
|
|
|
)
|
|
|
|
|
|
|
|
var fallbackDialer net.Dialer
|
|
|
|
|
|
|
|
// reuseErrShouldRetry diagnoses whether to retry after a reuse error.
|
|
|
|
// if we failed to bind, we should retry. if bind worked and this is a
|
|
|
|
// real dial error (remote end didnt answer) then we should not retry.
|
|
|
|
func reuseErrShouldRetry(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false // hey, it worked! no need to retry.
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a network timeout error, it's a legitimate failure.
|
|
|
|
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
errno, ok := err.(syscall.Errno)
|
|
|
|
if !ok { // not an errno? who knows what this is. retry.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
switch errno {
|
|
|
|
case syscall.EADDRINUSE, syscall.EADDRNOTAVAIL:
|
|
|
|
return true // failure to bind. retry.
|
|
|
|
case syscall.ECONNREFUSED:
|
|
|
|
return false // real dial error
|
|
|
|
default:
|
|
|
|
return true // optimistically default to retry.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// Dials using reuseport and then redials normally if that fails.
|
|
|
|
func reuseDial(ctx context.Context, laddr *net.TCPAddr, network, raddr string) (con net.Conn, err error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
if laddr == nil {
|
|
|
|
return fallbackDialer.DialContext(ctx, network, raddr)
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
d := net.Dialer{
|
|
|
|
LocalAddr: laddr,
|
|
|
|
Control: reuseport.Control,
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
con, err = d.DialContext(ctx, network, raddr)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err == nil {
|
|
|
|
return con, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if reuseErrShouldRetry(err) && ctx.Err() == nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
// We could have an existing socket open or we could have one
|
|
|
|
// stuck in TIME-WAIT.
|
2018-07-04 10:51:47 +00:00
|
|
|
log.Debugf("failed to reuse port, dialing with a random port: %s", err)
|
|
|
|
con, err = fallbackDialer.DialContext(ctx, network, raddr)
|
|
|
|
}
|
|
|
|
return con, err
|
|
|
|
}
|