refactor(netx): the TimeoutDialer is useless (#366)
We already configure a timeout in the underlying dialer, hence there's no point in keeping the TimeoutDialer around. Part of https://github.com/ooni/probe/issues/1507
This commit is contained in:
@@ -6,8 +6,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// defaultNetDialer is the net.Dialer we use by default.
|
||||
var defaultNetDialer = &net.Dialer{
|
||||
// underlyingDialer is the underlying net.Dialer.
|
||||
var underlyingDialer = &net.Dialer{
|
||||
Timeout: 15 * time.Second,
|
||||
KeepAlive: 15 * time.Second,
|
||||
}
|
||||
@@ -17,7 +17,7 @@ type SystemDialer struct{}
|
||||
|
||||
// DialContext implements Dialer.DialContext
|
||||
func (d SystemDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return defaultNetDialer.DialContext(ctx, network, address)
|
||||
return underlyingDialer.DialContext(ctx, network, address)
|
||||
}
|
||||
|
||||
// Default is the dialer we use by default.
|
||||
|
||||
@@ -3,11 +3,12 @@ package dialer
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/psiphon/oopsi/golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestSystemDialer(t *testing.T) {
|
||||
func TestSystemDialerWorks(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // fail immediately
|
||||
conn, err := Default.DialContext(ctx, "tcp", "8.8.8.8:853")
|
||||
@@ -18,3 +19,10 @@ func TestSystemDialer(t *testing.T) {
|
||||
t.Fatal("expected nil conn here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnderlyingDialerHasTimeout(t *testing.T) {
|
||||
expected := 15 * time.Second
|
||||
if underlyingDialer.Timeout != expected {
|
||||
t.Fatal("unexpected timeout value")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimeoutDialer is a Dialer that enforces a timeout
|
||||
type TimeoutDialer struct {
|
||||
Dialer
|
||||
ConnectTimeout time.Duration // default: 30 seconds
|
||||
}
|
||||
|
||||
// DialContext implements Dialer.DialContext
|
||||
func (d TimeoutDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
timeout := 30 * time.Second
|
||||
if d.ConnectTimeout != 0 {
|
||||
timeout = d.ConnectTimeout
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
return d.Dialer.DialContext(ctx, network, address)
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package dialer_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
|
||||
)
|
||||
|
||||
type SlowDialer struct{}
|
||||
|
||||
func (SlowDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-time.After(30 * time.Second):
|
||||
return nil, io.EOF
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeoutDialer(t *testing.T) {
|
||||
d := dialer.TimeoutDialer{Dialer: SlowDialer{}, ConnectTimeout: time.Second}
|
||||
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if conn != nil {
|
||||
t.Fatal("expected nil conn here")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user