Commit f431c22e authored by Steven Allen's avatar Steven Allen

define a global fallbackDialer

indirectly addresses:

https://github.com/libp2p/go-reuseport-transport/issues/1#issuecomment-376625179
parent cbbb9c33
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
reuseport "github.com/libp2p/go-reuseport" reuseport "github.com/libp2p/go-reuseport"
) )
var fallbackDialer net.Dialer
// reuseErrShouldRetry diagnoses whether to retry after a reuse error. // reuseErrShouldRetry diagnoses whether to retry after a reuse error.
// if we failed to bind, we should retry. if bind worked and this is a // 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. // real dial error (remote end didnt answer) then we should not retry.
...@@ -39,8 +41,7 @@ func reuseErrShouldRetry(err error) bool { ...@@ -39,8 +41,7 @@ func reuseErrShouldRetry(err error) bool {
// Dials using reusport and then redials normally if that fails. // Dials using reusport and then redials normally if that fails.
func reuseDial(ctx context.Context, laddr *net.TCPAddr, network, raddr string) (net.Conn, error) { func reuseDial(ctx context.Context, laddr *net.TCPAddr, network, raddr string) (net.Conn, error) {
if laddr == nil { if laddr == nil {
var d net.Dialer return fallbackDialer.DialContext(ctx, network, raddr)
return d.DialContext(ctx, network, raddr)
} }
d := reuseport.Dialer{ d := reuseport.Dialer{
...@@ -56,8 +57,7 @@ func reuseDial(ctx context.Context, laddr *net.TCPAddr, network, raddr string) ( ...@@ -56,8 +57,7 @@ func reuseDial(ctx context.Context, laddr *net.TCPAddr, network, raddr string) (
if reuseErrShouldRetry(err) && ctx.Err() == nil { if reuseErrShouldRetry(err) && ctx.Err() == nil {
log.Debugf("failed to reuse port, dialing with a random port: %s", err) log.Debugf("failed to reuse port, dialing with a random port: %s", err)
var d net.Dialer con, err = fallbackDialer.DialContext(ctx, network, raddr)
con, err = d.DialContext(ctx, network, raddr)
} }
return con, err return con, err
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment