Skip to content

Commit 6f8773a

Browse files
committed
ConnectAddress() was refactored
1 parent 280f0aa commit 6f8773a

19 files changed

+181
-73
lines changed

cassandra_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"context"
3333
"errors"
3434
"fmt"
35-
"github.com/stretchr/testify/require"
3635
"io"
3736
"math"
3837
"math/big"
@@ -45,7 +44,9 @@ import (
4544
"time"
4645
"unicode"
4746

48-
inf "gopkg.in/inf.v0"
47+
"github.com/stretchr/testify/require"
48+
49+
"gopkg.in/inf.v0"
4950
)
5051

5152
func TestEmptyHosts(t *testing.T) {
@@ -809,7 +810,11 @@ func TestReconnection(t *testing.T) {
809810
defer session.Close()
810811

811812
h := session.ring.allHosts()[0]
812-
session.handleNodeDown(h.ConnectAddress(), h.Port())
813+
connAddr, err := h.ConnectAddress()
814+
if err != nil {
815+
t.Fatal(err)
816+
}
817+
session.handleNodeDown(connAddr, h.Port())
813818

814819
if h.State() != NodeDown {
815820
t.Fatal("Host should be NodeDown but not.")
@@ -3086,7 +3091,11 @@ func TestDiscoverViaProxy(t *testing.T) {
30863091
for _, host := range clusterHosts {
30873092
found := false
30883093
for _, hi := range session.pool.hostConnPools {
3089-
if hi.host.ConnectAddress().String() == host {
3094+
connAddr, err := hi.host.ConnectAddress()
3095+
if err != nil {
3096+
t.Error(err)
3097+
}
3098+
if connAddr.String() == host {
30903099
found = true
30913100
break
30923101
}

conn.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,11 @@ func (c *Conn) awaitSchemaAgreement(ctx context.Context) (err error) {
16971697
}
16981698

16991699
for _, row := range rows {
1700-
host, err := c.session.hostInfoFromMap(row, &HostInfo{connectAddress: c.host.ConnectAddress(), port: c.session.cfg.Port})
1700+
connAddr, err := c.host.ConnectAddress()
1701+
if err != nil {
1702+
goto cont
1703+
}
1704+
host, err := c.session.hostInfoFromMap(row, &HostInfo{connectAddress: connAddr, port: c.session.cfg.Port})
17011705
if err != nil {
17021706
goto cont
17031707
}

conn_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ type testQueryObserver struct {
350350
}
351351

352352
func (o *testQueryObserver) ObserveQuery(ctx context.Context, q ObservedQuery) {
353-
host := q.Host.ConnectAddress().String()
353+
connAddr, _ := q.Host.ConnectAddress()
354+
host := connAddr.String()
354355
o.metrics[host] = q.Metrics
355356
if o.verbose {
356357
o.logger.Printf("Observed query %q. Returned %v rows, took %v on host %q with %v attempts and total latency %v. Error: %q\n",
@@ -359,7 +360,8 @@ func (o *testQueryObserver) ObserveQuery(ctx context.Context, q ObservedQuery) {
359360
}
360361

361362
func (o *testQueryObserver) GetMetrics(host *HostInfo) *hostMetrics {
362-
return o.metrics[host.ConnectAddress().String()]
363+
connAddr, _ := host.ConnectAddress()
364+
return o.metrics[connAddr.String()]
363365
}
364366

365367
// TestQueryRetry will test to make sure that gocql will execute

connectionpool.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,11 @@ func (pool *hostConnPool) logConnectErr(err error) {
497497

498498
// transition back to a not-filling state.
499499
func (pool *hostConnPool) fillingStopped(err error) {
500+
connAddr, _ := pool.host.ConnectAddress()
501+
500502
if err != nil {
501503
if gocqlDebug {
502-
pool.logger.Printf("gocql: filling stopped %q: %v\n", pool.host.ConnectAddress(), err)
504+
pool.logger.Printf("gocql: filling stopped %q: %v\n", connAddr, err)
503505
}
504506
// wait for some time to avoid back-to-back filling
505507
// this provides some time between failed attempts
@@ -517,11 +519,11 @@ func (pool *hostConnPool) fillingStopped(err error) {
517519
// if we errored and the size is now zero, make sure the host is marked as down
518520
// see https://github.com/apache/cassandra-gocql-driver/issues/1614
519521
if gocqlDebug {
520-
pool.logger.Printf("gocql: conns of pool after stopped %q: %v\n", host.ConnectAddress(), count)
522+
pool.logger.Printf("gocql: conns of pool after stopped %q: %v\n", connAddr, count)
521523
}
522524
if err != nil && count == 0 {
523525
if pool.session.cfg.ConvictionPolicy.AddFailure(err, host) {
524-
pool.session.handleNodeDown(host.ConnectAddress(), port)
526+
pool.session.handleNodeDown(connAddr, port)
525527
}
526528
}
527529
}
@@ -575,8 +577,9 @@ func (pool *hostConnPool) connect() (err error) {
575577
}
576578
}
577579
if gocqlDebug {
580+
connAddr, _ := pool.host.ConnectAddress()
578581
pool.logger.Printf("gocql: connection failed %q: %v, reconnecting with %T\n",
579-
pool.host.ConnectAddress(), err, reconnectionPolicy)
582+
connAddr, err, reconnectionPolicy)
580583
}
581584
time.Sleep(reconnectionPolicy.GetInterval(i))
582585
}

control.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,21 @@ func (c *controlConn) connect(hosts []*HostInfo) error {
261261
var conn *Conn
262262
var err error
263263
for _, host := range hosts {
264+
connAddr, err := host.ConnectAddress()
265+
if err != nil {
266+
c.session.logger.Printf("gocql: %v\n", err)
267+
continue
268+
}
264269
conn, err = c.session.dial(c.session.ctx, host, &cfg, c)
265270
if err != nil {
266-
c.session.logger.Printf("gocql: unable to dial control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
271+
c.session.logger.Printf("gocql: unable to dial control conn %v:%v: %v\n", connAddr, host.Port(), err)
267272
continue
268273
}
269274
err = c.setupConn(conn)
270275
if err == nil {
271276
break
272277
}
273-
c.session.logger.Printf("gocql: unable setup control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
278+
c.session.logger.Printf("gocql: unable setup control conn %v:%v: %v\n", connAddr, host.Port(), err)
274279
conn.Close()
275280
conn = nil
276281
}
@@ -300,9 +305,13 @@ func (c *controlConn) setupConn(conn *Conn) error {
300305
}
301306

302307
host = c.session.ring.addOrUpdate(host)
308+
connAddr, err := host.ConnectAddress()
309+
if err != nil {
310+
return err
311+
}
303312

304313
if c.session.cfg.filterHost(host) {
305-
return fmt.Errorf("host was filtered: %v", host.ConnectAddress())
314+
return fmt.Errorf("host was filtered: %v", connAddr)
306315
}
307316

308317
if err := c.registerEvents(conn); err != nil {
@@ -423,16 +432,21 @@ func (c *controlConn) attemptReconnectToAnyOfHosts(hosts []*HostInfo) (*Conn, er
423432
var conn *Conn
424433
var err error
425434
for _, host := range hosts {
435+
connAddr, err := host.ConnectAddress()
436+
if err != nil {
437+
c.session.logger.Printf("gocql: %v\n", err)
438+
continue
439+
}
426440
conn, err = c.session.connect(c.session.ctx, host, c)
427441
if err != nil {
428-
c.session.logger.Printf("gocql: unable to dial control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
442+
c.session.logger.Printf("gocql: unable to dial control conn %v:%v: %v\n", connAddr, host.Port(), err)
429443
continue
430444
}
431445
err = c.setupConn(conn)
432446
if err == nil {
433447
break
434448
}
435-
c.session.logger.Printf("gocql: unable setup control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
449+
c.session.logger.Printf("gocql: unable setup control conn %v:%v: %v\n", connAddr, host.Port(), err)
436450
conn.Close()
437451
conn = nil
438452
}

control_ccm_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ type TestHostFilter struct {
4444
func (f *TestHostFilter) Accept(h *HostInfo) bool {
4545
f.mu.Lock()
4646
defer f.mu.Unlock()
47-
_, ok := f.allowedHosts[h.ConnectAddress().String()]
47+
connAddr, _ := h.ConnectAddress()
48+
_, ok := f.allowedHosts[connAddr.String()]
4849
return ok
4950
}
5051

@@ -100,7 +101,8 @@ func TestControlConn_ReconnectRefreshesRing(t *testing.T) {
100101

101102
var ccHostName string
102103
for _, node := range allCcmHosts {
103-
if node.Addr == ccHost.ConnectAddress().String() {
104+
connAddr, _ := ccHost.ConnectAddress()
105+
if node.Addr == connAddr.String() {
104106
ccHostName = node.Name
105107
break
106108
}

control_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ func TestHostInfo_Lookup(t *testing.T) {
4949
}
5050

5151
host := hosts[0]
52-
if !host.ConnectAddress().Equal(test.ip) {
53-
t.Errorf("expected ip %v got %v for addr %q", test.ip, host.ConnectAddress(), test.addr)
52+
connAddr, err := host.ConnectAddress()
53+
if err != nil {
54+
t.Errorf("%d: %v", i, err)
55+
continue
56+
}
57+
if !connAddr.Equal(test.ip) {
58+
t.Errorf("expected ip %v got %v for addr %q", test.ip, connAddr, test.addr)
5459
}
5560
}
5661
}

dial.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ type defaultHostDialer struct {
6060
}
6161

6262
func (hd *defaultHostDialer) DialHost(ctx context.Context, host *HostInfo) (*DialedHost, error) {
63-
ip := host.ConnectAddress()
63+
ip, err := host.ConnectAddress()
64+
if err != nil {
65+
return nil, err
66+
}
6467
port := host.Port()
6568

6669
if !validIpAddr(ip) {

events.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ func (s *Session) startPoolFill(host *HostInfo) {
241241

242242
func (s *Session) handleNodeConnected(host *HostInfo) {
243243
if gocqlDebug {
244-
s.logger.Printf("gocql: Session.handleNodeConnected: %s:%d\n", host.ConnectAddress(), host.Port())
244+
connAddr, _ := host.ConnectAddress()
245+
s.logger.Printf("gocql: Session.handleNodeConnected: %s:%d\n", connAddr, host.Port())
245246
}
246247

247248
host.setState(NodeUp)

filters.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ func WhiteListHostFilter(hosts ...string) HostFilter {
7272

7373
m := make(map[string]bool, len(hostInfos))
7474
for _, host := range hostInfos {
75-
m[host.ConnectAddress().String()] = true
75+
connAddr, _ := host.ConnectAddress()
76+
m[connAddr.String()] = true
7677
}
7778

7879
return HostFilterFunc(func(host *HostInfo) bool {
79-
return m[host.ConnectAddress().String()]
80+
connAddr, _ := host.ConnectAddress()
81+
return m[connAddr.String()]
82+
8083
})
8184
}

0 commit comments

Comments
 (0)