浏览代码

Cleaner address/port parsing

Josh Wright 11 年之前
父节点
当前提交
24dd1a86e5
共有 3 个文件被更改,包括 6 次插入19 次删除
  1. 0 7
      conn.go
  2. 6 8
      connectionpool.go
  3. 0 4
      host_source.go

+ 0 - 7
conn.go

@@ -12,7 +12,6 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net"
-	"strings"
 	"sync"
 	"sync/atomic"
 	"time"
@@ -100,12 +99,6 @@ 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

+ 6 - 8
connectionpool.go

@@ -1,9 +1,9 @@
 package gocql
 
 import (
-	"fmt"
 	"log"
-	"regexp"
+	"net"
+	"strconv"
 	"strings"
 	"sync"
 	"time"
@@ -146,9 +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])
-		port_appended, _ := regexp.MatchString(`:\d*$`, addr)
-		if !port_appended {
-			addr = fmt.Sprintf("%s:%d", addr, cfg.Port)
+		if _, _, err := net.SplitHostPort(addr); err != nil {
+			addr = net.JoinHostPort(addr, strconv.Itoa(cfg.Port))
 		}
 
 		if pool.connect(addr) == nil {
@@ -238,9 +237,8 @@ func (c *SimplePool) fillPool() {
 	//Walk through list of defined hosts
 	for host := range c.hosts {
 		addr := strings.TrimSpace(host)
-		port_appended, _ := regexp.MatchString(`:\d*$`, addr)
-		if !port_appended {
-			addr = fmt.Sprintf("%s:%d", addr, c.cfg.Port)
+		if _, _, err := net.SplitHostPort(addr); err != nil {
+			addr = net.JoinHostPort(addr, strconv.Itoa(c.cfg.Port))
 		}
 
 		numConns := 1

+ 0 - 4
host_source.go

@@ -3,7 +3,6 @@ package gocql
 import (
 	"log"
 	"net"
-	"strings"
 	"time"
 )
 
@@ -57,9 +56,6 @@ 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)
 		}
 	}