From d3381c10c43fafbcf7dba191f6f2d1c917165544 Mon Sep 17 00:00:00 2001 From: Josh Wright Date: Thu, 20 Nov 2014 14:21:19 -0500 Subject: [PATCH] Handle host discovery in clusters with IPv6 addresses Cassandra doesn't wrap IPv6 addresses in brackets when querying system.peers, and the old method of checking for a port appended to the address didn't account for the extra colons in IPv6 addresses. --- conn.go | 7 +++++++ connectionpool.go | 7 +++++-- host_source.go | 4 ++++ 3 files changed, 16 insertions(+), 2 deletions(-) mode change 100644 => 100755 conn.go mode change 100644 => 100755 connectionpool.go mode change 100644 => 100755 host_source.go diff --git a/conn.go b/conn.go old mode 100644 new mode 100755 index e05668e1b..cfa938612 --- a/conn.go +++ b/conn.go @@ -12,6 +12,7 @@ import ( "fmt" "io/ioutil" "net" + "strings" "sync" "sync/atomic" "time" @@ -99,6 +100,12 @@ func Connect(addr string, cfg ConnConfig, pool ConnectionPool) (*Conn, error) { conn net.Conn ) + address := addr[:strings.LastIndex(addr, ":")] + port := addr[strings.LastIndex(addr, ":")+1:] + if strings.Count(address, ":") > 1 && strings.Index(address, "[") < 0 { + addr = fmt.Sprintf("[%s]:%s", address, port) + } + if cfg.SslOpts != nil { certPool := x509.NewCertPool() //ca cert is optional diff --git a/connectionpool.go b/connectionpool.go old mode 100644 new mode 100755 index f743e8b72..dd470cc1c --- a/connectionpool.go +++ b/connectionpool.go @@ -3,6 +3,7 @@ package gocql import ( "fmt" "log" + "regexp" "strings" "sync" "time" @@ -145,7 +146,8 @@ func NewSimplePool(cfg *ClusterConfig) ConnectionPool { //defer the remaining connections to cluster.fillPool() for i := 0; i < len(cfg.Hosts); i++ { addr := strings.TrimSpace(cfg.Hosts[i]) - if strings.Index(addr, ":") < 0 { + port_appended, _ := regexp.MatchString(`:\d*$`, addr) + if !port_appended { addr = fmt.Sprintf("%s:%d", addr, cfg.Port) } @@ -236,7 +238,8 @@ func (c *SimplePool) fillPool() { //Walk through list of defined hosts for host := range c.hosts { addr := strings.TrimSpace(host) - if strings.Index(addr, ":") < 0 { + port_appended, _ := regexp.MatchString(`:\d*$`, addr) + if !port_appended { addr = fmt.Sprintf("%s:%d", addr, c.cfg.Port) } diff --git a/host_source.go b/host_source.go old mode 100644 new mode 100755 index b4c76f7d2..f1d5deaa5 --- a/host_source.go +++ b/host_source.go @@ -3,6 +3,7 @@ package gocql import ( "log" "net" + "strings" "time" ) @@ -56,6 +57,9 @@ func (r *ringDescriber) GetHosts() ([]HostInfo, error) { for iter.Scan(&host.Peer, &host.DataCenter, &host.Rack, &host.HostId, &host.Tokens) { if r.matchFilter(host) { + if strings.Count(host.Peer, ":") > 1 { + host.Peer = "[" + host.Peer + "]" + } hosts = append(hosts, *host) } }