Skip to content

Commit 37fd6b7

Browse files
committed
refactor(netxlite): use *Netx for the system dialer
This diff is like 7224984 except that we are converting the system dialer constructors to use *Netx rather than converting the system resolver. While there, realize that DialerSystem could become private. Part of ooni/probe#2531
1 parent 7224984 commit 37fd6b7

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

internal/netxlite/dialer.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ func NewDialerWithStdlibResolver(dl model.DebugLogger) model.Dialer {
2222
return NewDialerWithResolver(dl, reso)
2323
}
2424

25-
// NewDialerWithResolver is equivalent to calling WrapDialer with
26-
// the dialer argument being equal to &DialerSystem{}.
25+
// NewDialerWithResolver is equivalent to calling WrapDialer with a dialer using the stdlib
26+
// where the underlying dialer uses the [*Netx] UnderlyingNetwork for dialing.
27+
func (netx *Netx) NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...model.DialerWrapper) model.Dialer {
28+
return WrapDialer(dl, r, &dialerSystem{provider: netx.maybeCustomUnderlyingNetwork()}, w...)
29+
}
30+
31+
// NewDialerWithResolver is equivalent to creating an empty [*Netx]
32+
// and callings its NewDialerWithResolver method.
2733
func NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...model.DialerWrapper) model.Dialer {
28-
return WrapDialer(dl, r, &DialerSystem{}, w...)
34+
netx := &Netx{Underlying: nil}
35+
return netx.NewDialerWithResolver(dl, r, w...)
2936
}
3037

3138
// WrapDialer wraps an existing Dialer to add extra functionality
@@ -142,24 +149,24 @@ func NewDialerWithoutResolver(dl model.DebugLogger, w ...model.DialerWrapper) mo
142149
return NewDialerWithResolver(dl, &NullResolver{}, w...)
143150
}
144151

145-
// DialerSystem is a model.Dialer that uses the stdlib's net.Dialer
152+
// dialerSystem is a model.Dialer that uses the stdlib's net.Dialer
146153
// to construct the new SimpleDialer used for dialing. This dialer has
147154
// a fixed timeout for each connect operation equal to 15 seconds.
148-
type DialerSystem struct {
155+
type dialerSystem struct {
149156
// provider is the OPTIONAL nil-safe [model.UnderlyingNetwork] provider.
150157
provider *MaybeCustomUnderlyingNetwork
151158
}
152159

153-
var _ model.Dialer = &DialerSystem{}
160+
var _ model.Dialer = &dialerSystem{}
154161

155-
func (d *DialerSystem) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
162+
func (d *dialerSystem) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
156163
p := d.provider.Get()
157164
ctx, cancel := context.WithTimeout(ctx, p.DialTimeout())
158165
defer cancel()
159166
return p.DialContext(ctx, network, address)
160167
}
161168

162-
func (d *DialerSystem) CloseIdleConnections() {
169+
func (d *dialerSystem) CloseIdleConnections() {
163170
// nothing to do here
164171
}
165172

internal/netxlite/dialer_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestNewDialerWithStdlibResolver(t *testing.T) {
3131
t.Fatal("invalid logger")
3232
}
3333
errWrapper := logger.Dialer.(*dialerErrWrapper)
34-
_ = errWrapper.Dialer.(*DialerSystem)
34+
_ = errWrapper.Dialer.(*dialerSystem)
3535
}
3636

3737
type extensionDialerFirst struct {
@@ -76,19 +76,19 @@ func TestNewDialer(t *testing.T) {
7676
ext2 := logger.Dialer.(*extensionDialerSecond)
7777
ext1 := ext2.Dialer.(*extensionDialerFirst)
7878
errWrapper := ext1.Dialer.(*dialerErrWrapper)
79-
_ = errWrapper.Dialer.(*DialerSystem)
79+
_ = errWrapper.Dialer.(*dialerSystem)
8080
})
8181
}
8282

8383
func TestDialerSystem(t *testing.T) {
8484
t.Run("CloseIdleConnections", func(t *testing.T) {
85-
d := &DialerSystem{}
85+
d := &dialerSystem{}
8686
d.CloseIdleConnections() // to avoid missing coverage
8787
})
8888

8989
t.Run("DialContext", func(t *testing.T) {
9090
t.Run("with canceled context", func(t *testing.T) {
91-
d := &DialerSystem{}
91+
d := &dialerSystem{}
9292
ctx, cancel := context.WithCancel(context.Background())
9393
cancel() // immediately!
9494
conn, err := d.DialContext(ctx, "tcp", "8.8.8.8:443")
@@ -108,7 +108,7 @@ func TestDialerSystem(t *testing.T) {
108108
},
109109
MockDialContext: defaultTp.DialContext,
110110
}
111-
d := &DialerSystem{provider: &MaybeCustomUnderlyingNetwork{tp}}
111+
d := &dialerSystem{provider: &MaybeCustomUnderlyingNetwork{tp}}
112112
ctx := context.Background()
113113
start := time.Now()
114114
conn, err := d.DialContext(ctx, "tcp", "dns.google:443")
@@ -134,7 +134,7 @@ func TestDialerSystem(t *testing.T) {
134134
return nil, expected
135135
},
136136
}
137-
d := &DialerSystem{provider: &MaybeCustomUnderlyingNetwork{proxy}}
137+
d := &dialerSystem{provider: &MaybeCustomUnderlyingNetwork{proxy}}
138138
conn, err := d.DialContext(context.Background(), "tcp", "dns.google:443")
139139
if conn != nil {
140140
t.Fatal("unexpected conn")
@@ -150,7 +150,7 @@ func TestDialerResolverWithTracing(t *testing.T) {
150150
t.Run("DialContext", func(t *testing.T) {
151151
t.Run("fails without a port", func(t *testing.T) {
152152
d := &dialerResolverWithTracing{
153-
Dialer: &DialerSystem{},
153+
Dialer: &dialerSystem{},
154154
Resolver: NewUnwrappedStdlibResolver(),
155155
}
156156
const missingPort = "ooni.nu"
@@ -497,7 +497,7 @@ func TestDialerResolverWithTracing(t *testing.T) {
497497
t.Run("lookupHost", func(t *testing.T) {
498498
t.Run("handles addresses correctly", func(t *testing.T) {
499499
dialer := &dialerResolverWithTracing{
500-
Dialer: &DialerSystem{},
500+
Dialer: &dialerSystem{},
501501
Resolver: &NullResolver{},
502502
}
503503
addrs, err := dialer.lookupHost(context.Background(), "1.1.1.1")
@@ -511,7 +511,7 @@ func TestDialerResolverWithTracing(t *testing.T) {
511511

512512
t.Run("fails correctly on lookup error", func(t *testing.T) {
513513
dialer := &dialerResolverWithTracing{
514-
Dialer: &DialerSystem{},
514+
Dialer: &dialerSystem{},
515515
Resolver: &NullResolver{},
516516
}
517517
ctx := context.Background()

internal/netxlite/netx.go

-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ func (netx *Netx) maybeCustomUnderlyingNetwork() *MaybeCustomUnderlyingNetwork {
2222
return &MaybeCustomUnderlyingNetwork{netx.Underlying}
2323
}
2424

25-
// NewDialerWithResolver is like [netxlite.NewDialerWithResolver] but the constructed [model.Dialer]
26-
// uses the [model.UnderlyingNetwork] configured inside the [Netx] structure.
27-
func (n *Netx) NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...model.DialerWrapper) model.Dialer {
28-
return WrapDialer(dl, r, &DialerSystem{provider: n.maybeCustomUnderlyingNetwork()}, w...)
29-
}
30-
3125
// NewUDPListener is like [netxlite.NewUDPListener] but the constructed [model.UDPListener]
3226
// uses the [model.UnderlyingNetwork] configured inside the [Netx] structure.
3327
func (n *Netx) NewUDPListener() model.UDPListener {

internal/netxlite/tls_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ func TestTLSDialer(t *testing.T) {
711711
t.Run("failure dialing", func(t *testing.T) {
712712
ctx, cancel := context.WithCancel(context.Background())
713713
cancel() // immediately fail
714-
dialer := tlsDialer{Dialer: &DialerSystem{}}
714+
dialer := tlsDialer{Dialer: &dialerSystem{}}
715715
conn, err := dialer.DialTLSContext(ctx, "tcp", "www.google.com:443")
716716
if err == nil || !strings.HasSuffix(err.Error(), "operation was canceled") {
717717
t.Fatal("not the error we expected", err)

0 commit comments

Comments
 (0)