Skip to content

Commit 40de973

Browse files
committed
connmgr: Remove unused DialAddr.
This removes all code related to DialAddr from the connection manager. Now the the connection manager is internal and the main code does not make use of DialAddr, there is no reason to keep a bunch of unused and unneeded code around.
1 parent 6ba66b0 commit 40de973

4 files changed

Lines changed: 27 additions & 120 deletions

File tree

internal/connmgr/connmanager.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,9 @@ type Config struct {
151151
// to. If nil, no new connections will be made automatically.
152152
GetNewAddress func() (net.Addr, error)
153153

154-
// Dial connects to the address on the named network. Either Dial or
155-
// DialAddr need to be specified (but not both).
154+
// Dial connects to the address on the named network.
156155
Dial func(ctx context.Context, network, addr string) (net.Conn, error)
157156

158-
// DialAddr is an alternative to Dial which receives a full net.Addr instead
159-
// of just the protocol family and address. Either DialAddr or Dial need
160-
// to be specified (but not both).
161-
DialAddr func(context.Context, net.Addr) (net.Conn, error)
162-
163157
// Timeout specifies the amount of time to wait for a connection
164158
// to complete before giving up.
165159
Timeout time.Duration
@@ -388,12 +382,7 @@ func (cm *ConnManager) Connect(ctx context.Context, c *ConnReq) {
388382
defer cancel()
389383
}
390384
var conn net.Conn
391-
var err error
392-
if cm.cfg.Dial != nil {
393-
conn, err = cm.cfg.Dial(ctx, c.Addr.Network(), c.Addr.String())
394-
} else {
395-
conn, err = cm.cfg.DialAddr(ctx, c.Addr)
396-
}
385+
conn, err := cm.cfg.Dial(ctx, c.Addr.Network(), c.Addr.String())
397386
if err != nil {
398387
cm.connMtx.Lock()
399388
cm.handleFailedPending(ctx, c, err)
@@ -648,13 +637,9 @@ func (cm *ConnManager) Run(ctx context.Context) {
648637
//
649638
// Use Run to start listening and/or connecting to the network.
650639
func New(cfg *Config) (*ConnManager, error) {
651-
if cfg.Dial == nil && cfg.DialAddr == nil {
640+
if cfg.Dial == nil {
652641
return nil, MakeError(ErrDialNil, "dial cannot be nil")
653642
}
654-
if cfg.Dial != nil && cfg.DialAddr != nil {
655-
return nil, MakeError(ErrBothDialsFilled,
656-
"cannot specify both Dial and DialAddr")
657-
}
658643
// Default to sane values
659644
if cfg.RetryDuration <= 0 {
660645
cfg.RetryDuration = defaultRetryDuration

internal/connmgr/connmanager_test.go

Lines changed: 9 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,6 @@ func mockDialer(ctx context.Context, network, addr string) (net.Conn, error) {
8686
return c, ctx.Err()
8787
}
8888

89-
// mockDialer mocks the net.Dial interface by returning a mock connection to
90-
// the given address.
91-
func mockDialerAddr(ctx context.Context, addr net.Addr) (net.Conn, error) {
92-
r, w := io.Pipe()
93-
c := &mockConn{rAddr: addr}
94-
c.Reader = r
95-
c.Writer = w
96-
return c, nil
97-
}
98-
9989
// TestNewConfig tests that new ConnManager config is validated as expected.
10090
func TestNewConfig(t *testing.T) {
10191
_, err := New(&Config{})
@@ -108,21 +98,6 @@ func TestNewConfig(t *testing.T) {
10898
if err != nil {
10999
t.Fatalf("New unexpected error: %v", err)
110100
}
111-
112-
_, err = New(&Config{
113-
Dial: mockDialer,
114-
DialAddr: mockDialerAddr,
115-
})
116-
if err == nil {
117-
t.Fatal("New expected error: 'Dial and DialAddr can't be both nil', got nil")
118-
}
119-
120-
_, err = New(&Config{
121-
DialAddr: mockDialerAddr,
122-
})
123-
if err != nil {
124-
t.Fatalf("New unexpected error: %v", err)
125-
}
126101
}
127102

128103
// assertConnReqID ensures the provided connection request has the given ID.
@@ -247,54 +222,6 @@ func TestTargetOutbound(t *testing.T) {
247222
wg.Wait()
248223
}
249224

250-
// TestPassAddrAlongDialAddr tests if when using the DialAddr config option,
251-
// any address object returned by GetNewAddress will be correctly passed along
252-
// to DialAddr to be used for connecting to a host.
253-
func TestPassAddrAlongDialAddr(t *testing.T) {
254-
dailedAddr := make(chan net.Addr)
255-
detectDialer := func(ctx context.Context, addr net.Addr) (net.Conn, error) {
256-
dailedAddr <- addr
257-
return nil, errors.New("error")
258-
}
259-
260-
// targetAddr will be the specific address we'll use to connect. It _could_
261-
// be carrying more info than a standard (tcp/udp) network address, so it
262-
// needs to be relayed to dialAddr.
263-
targetAddr := mockAddr{
264-
net: "invalid",
265-
address: "unreachable",
266-
}
267-
268-
cmgr, err := New(&Config{
269-
TargetOutbound: 1,
270-
DialAddr: detectDialer,
271-
GetNewAddress: func() (net.Addr, error) {
272-
return targetAddr, nil
273-
},
274-
})
275-
if err != nil {
276-
t.Fatalf("New error: %v", err)
277-
}
278-
_, shutdown, wg := runConnMgrAsync(context.Background(), cmgr)
279-
280-
select {
281-
case addr := <-dailedAddr:
282-
receivedMock, isMockAddr := addr.(mockAddr)
283-
if !isMockAddr {
284-
t.Fatal("connected to an address that was not a mockAddr")
285-
}
286-
if receivedMock != targetAddr {
287-
t.Fatal("connected to an address different than the expected target")
288-
}
289-
case <-time.After(time.Millisecond * 20):
290-
t.Fatal("did not get connection to target address before timeout")
291-
}
292-
293-
// Ensure clean shutdown of connection manager.
294-
shutdown()
295-
wg.Wait()
296-
}
297-
298225
// TestRetryPermanent tests that permanent connection requests are retried.
299226
//
300227
// We make a permanent connection request using Connect, disconnect it using
@@ -588,13 +515,13 @@ func TestRemovePendingConnection(t *testing.T) {
588515
// succeed.
589516
dialed := make(chan struct{})
590517
wait := make(chan struct{})
591-
indefiniteDialer := func(ctx context.Context, addr net.Addr) (net.Conn, error) {
518+
indefiniteDialer := func(ctx context.Context, network, addr string) (net.Conn, error) {
592519
close(dialed)
593520
<-wait
594521
return nil, errors.New("error")
595522
}
596523
cmgr, err := New(&Config{
597-
DialAddr: indefiniteDialer,
524+
Dial: indefiniteDialer,
598525
})
599526
if err != nil {
600527
t.Fatalf("New error: %v", err)
@@ -646,10 +573,10 @@ func TestCancelIgnoreDelayedConnection(t *testing.T) {
646573
// connect chan is signaled. The dial attempt immediately after that
647574
// will succeed in returning a connection.
648575
connect := make(chan struct{})
649-
failingDialer := func(ctx context.Context, addr net.Addr) (net.Conn, error) {
576+
failingDialer := func(ctx context.Context, network, addr string) (net.Conn, error) {
650577
select {
651578
case <-connect:
652-
return mockDialerAddr(ctx, addr)
579+
return mockDialer(ctx, network, addr)
653580
default:
654581
}
655582

@@ -658,7 +585,7 @@ func TestCancelIgnoreDelayedConnection(t *testing.T) {
658585

659586
connected := make(chan *ConnReq)
660587
cmgr, err := New(&Config{
661-
DialAddr: failingDialer,
588+
Dial: failingDialer,
662589
RetryDuration: retryTimeout,
663590
OnConnection: func(c *ConnReq, conn net.Conn) {
664591
connected <- c
@@ -822,17 +749,17 @@ func TestForEachConnReq(t *testing.T) {
822749
targetOutbound := uint32(5)
823750
connected := make(chan *ConnReq)
824751
pending := make(chan struct{})
825-
delayDialer := func(ctx context.Context, addr net.Addr) (net.Conn, error) {
826-
if addr.String() == "127.0.0.1:18557" {
752+
delayDialer := func(ctx context.Context, network, addr string) (net.Conn, error) {
753+
if addr == "127.0.0.1:18557" {
827754
close(pending)
828755
time.Sleep(time.Second)
829756
return nil, errors.New("error")
830757
}
831-
return mockDialerAddr(ctx, addr)
758+
return mockDialer(ctx, network, addr)
832759
}
833760
cmgr, err := New(&Config{
834761
TargetOutbound: targetOutbound,
835-
DialAddr: delayDialer,
762+
Dial: delayDialer,
836763
GetNewAddress: func() (net.Addr, error) {
837764
return &net.TCPAddr{
838765
IP: net.ParseIP("127.0.0.1"),

internal/connmgr/error.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ const (
1414
// the configuration.
1515
ErrDialNil = ErrorKind("ErrDialNil")
1616

17-
// ErrBothDialsFilled is used to indicate that Dial and DialAddr
18-
// cannot both be specified in the configuration.
19-
ErrBothDialsFilled = ErrorKind("ErrBothDialsFilled")
20-
2117
// ErrNotFound indicates a specified connection ID or address is unknown to
2218
// the connection manager.
2319
ErrNotFound = ErrorKind("ErrNotFound")

internal/connmgr/error_test.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func TestErrorKindStringer(t *testing.T) {
1717
want string
1818
}{
1919
{ErrDialNil, "ErrDialNil"},
20-
{ErrBothDialsFilled, "ErrBothDialsFilled"},
2120
{ErrNotFound, "ErrNotFound"},
2221
{ErrTorInvalidAddressResponse, "ErrTorInvalidAddressResponse"},
2322
{ErrTorInvalidProxyResponse, "ErrTorInvalidProxyResponse"},
@@ -91,35 +90,35 @@ func TestErrorKindIsAs(t *testing.T) {
9190
wantMatch: true,
9291
wantAs: ErrDialNil,
9392
}, {
94-
name: "ErrBothDialsFilled != ErrDialNil",
95-
err: ErrBothDialsFilled,
93+
name: "ErrNotFound != ErrDialNil",
94+
err: ErrNotFound,
9695
target: ErrDialNil,
9796
wantMatch: false,
98-
wantAs: ErrBothDialsFilled,
97+
wantAs: ErrNotFound,
9998
}, {
100-
name: "Error.ErrBothDialsFilled != ErrDialNil",
101-
err: MakeError(ErrBothDialsFilled, ""),
99+
name: "Error.ErrNotFound != ErrDialNil",
100+
err: MakeError(ErrNotFound, ""),
102101
target: ErrDialNil,
103102
wantMatch: false,
104-
wantAs: ErrBothDialsFilled,
103+
wantAs: ErrNotFound,
105104
}, {
106-
name: "ErrBothDialsFilled != Error.ErrDialNil",
107-
err: ErrBothDialsFilled,
105+
name: "ErrNotFound != Error.ErrDialNil",
106+
err: ErrNotFound,
108107
target: MakeError(ErrDialNil, ""),
109108
wantMatch: false,
110-
wantAs: ErrBothDialsFilled,
109+
wantAs: ErrNotFound,
111110
}, {
112-
name: "Error.ErrBothDialsFilled != Error.ErrDialNil",
113-
err: MakeError(ErrBothDialsFilled, ""),
111+
name: "Error.ErrNotFound != Error.ErrDialNil",
112+
err: MakeError(ErrNotFound, ""),
114113
target: MakeError(ErrDialNil, ""),
115114
wantMatch: false,
116-
wantAs: ErrBothDialsFilled,
115+
wantAs: ErrNotFound,
117116
}, {
118-
name: "Error.ErrBothDialsFilled != io.EOF",
119-
err: MakeError(ErrBothDialsFilled, ""),
117+
name: "Error.ErrNotFound != io.EOF",
118+
err: MakeError(ErrNotFound, ""),
120119
target: io.EOF,
121120
wantMatch: false,
122-
wantAs: ErrBothDialsFilled,
121+
wantAs: ErrNotFound,
123122
}}
124123

125124
for _, test := range tests {

0 commit comments

Comments
 (0)