From eb9f01f8f302217f1227f307ad3ab71013f4b9f8 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Fri, 2 Mar 2018 14:17:20 -0500 Subject: [PATCH] connmgr: Use same Dial func signature as net.Dial --- config.go | 8 ++++---- connmgr/connmanager.go | 6 +++--- connmgr/connmanager_test.go | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/config.go b/config.go index 86e9c98389..9fda612f1a 100644 --- a/config.go +++ b/config.go @@ -1126,11 +1126,11 @@ func loadConfig() (*config, []string, error) { // example, .onion addresses will be dialed using the onion specific proxy if // one was specified, but will otherwise use the normal dial function (which // could itself use a proxy or not). -func dcrdDial(addr net.Addr) (net.Conn, error) { - if strings.Contains(addr.String(), ".onion:") { - return cfg.oniondial(addr.Network(), addr.String()) +func dcrdDial(network, addr string) (net.Conn, error) { + if strings.Contains(addr, ".onion:") { + return cfg.oniondial(network, addr) } - return cfg.dial(addr.Network(), addr.String()) + return cfg.dial(network, addr) } // dcrdLookup returns the correct DNS lookup function to use depending on the diff --git a/connmgr/connmanager.go b/connmgr/connmanager.go index 649c84ac04..c3369a22d1 100644 --- a/connmgr/connmanager.go +++ b/connmgr/connmanager.go @@ -20,7 +20,7 @@ import ( const maxFailedAttempts = 25 var ( - //ErrDialNil is used to indicate that Dial cannot be nil in the configuration. + // ErrDialNil is used to indicate that Dial cannot be nil in the configuration. ErrDialNil = errors.New("config: dial cannot be nil") // maxRetryDuration is the max duration of time retrying of a persistent @@ -134,7 +134,7 @@ type Config struct { GetNewAddress func() (net.Addr, error) // Dial connects to the address on the named network. It cannot be nil. - Dial func(net.Addr) (net.Conn, error) + Dial func(network, addr string) (net.Conn, error) } // handleConnected is used to queue a successful connection. @@ -300,7 +300,7 @@ func (cm *ConnManager) Connect(c *ConnReq) { atomic.StoreUint64(&c.id, atomic.AddUint64(&cm.connReqCount, 1)) } log.Debugf("Attempting to connect to %v", c) - conn, err := cm.cfg.Dial(c.Addr) + conn, err := cm.cfg.Dial(c.Addr.Network(), c.Addr.String()) if err != nil { cm.requests <- handleFailed{c, err} } else { diff --git a/connmgr/connmanager_test.go b/connmgr/connmanager_test.go index 03b6dd2e0f..797a85d3fb 100644 --- a/connmgr/connmanager_test.go +++ b/connmgr/connmanager_test.go @@ -60,9 +60,9 @@ func (c mockConn) SetWriteDeadline(t time.Time) error { return nil } // mockDialer mocks the net.Dial interface by returning a mock connection to // the given address. -func mockDialer(addr net.Addr) (net.Conn, error) { +func mockDialer(network, addr string) (net.Conn, error) { r, w := io.Pipe() - c := &mockConn{rAddr: addr} + c := &mockConn{rAddr: &mockAddr{network, addr}} c.Reader = r c.Writer = w return c, nil @@ -307,10 +307,10 @@ func TestMaxRetryDuration(t *testing.T) { time.AfterFunc(5*time.Millisecond, func() { close(networkUp) }) - timedDialer := func(addr net.Addr) (net.Conn, error) { + timedDialer := func(network, addr string) (net.Conn, error) { select { case <-networkUp: - return mockDialer(addr) + return mockDialer(network, addr) default: return nil, errors.New("network down") } @@ -352,7 +352,7 @@ func TestMaxRetryDuration(t *testing.T) { // failure gracefully. func TestNetworkFailure(t *testing.T) { var dials uint32 - errDialer := func(net net.Addr) (net.Conn, error) { + errDialer := func(network, addr string) (net.Conn, error) { atomic.AddUint32(&dials, 1) return nil, errors.New("network down") } @@ -391,7 +391,7 @@ func TestNetworkFailure(t *testing.T) { // the failure. func TestStopFailed(t *testing.T) { done := make(chan struct{}, 1) - waitDialer := func(addr net.Addr) (net.Conn, error) { + waitDialer := func(network, addr string) (net.Conn, error) { done <- struct{}{} time.Sleep(time.Millisecond) return nil, errors.New("network down")