Skip to content

Commit

Permalink
Handle host discovery in clusters with IPv6 addresses
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JshWright committed Nov 20, 2014
1 parent 243807f commit d3381c1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions conn.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"io/ioutil"
"net"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions connectionpool.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gocql
import (
"fmt"
"log"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions host_source.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gocql
import (
"log"
"net"
"strings"
"time"
)

Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit d3381c1

Please sign in to comment.