فهرست منبع

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.
Josh Wright 11 سال پیش
والد
کامیت
d3381c10c4
3فایلهای تغییر یافته به همراه16 افزوده شده و 2 حذف شده
  1. 7 0
      conn.go
  2. 5 2
      connectionpool.go
  3. 4 0
      host_source.go

+ 7 - 0
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

+ 5 - 2
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)
 		}
 

+ 4 - 0
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)
 		}
 	}