Skip to content

Commit

Permalink
Fix IPv6 address connection (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
sitingren authored Jan 13, 2023
1 parent eaa5bd6 commit a9e0685
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:

steps:
- name: Check out repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Set up a Vertica server
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ where *myDBConnectString* is of the form:
```Go
vertica://(user):(password)@(host):(port)/(database)?(queryArgs)
```
If the host is a literal IPv6 address it must be enclosed in square brackets.

Currently supported query arguments are:

Expand Down
10 changes: 2 additions & 8 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (v *connection) establishSocketConnection() (net.Conn, error) {
host, port, err := net.SplitHostPort(v.connHostsList[i])
if err != nil {
// no host-port pair identified
err_msg += fmt.Sprintf("\n '%s': %s", host, err.Error())
err_msg += fmt.Sprintf("\n '%s': %s", v.connHostsList[i], err.Error())
continue
}
ips, err := net.LookupIP(host)
Expand All @@ -305,14 +305,8 @@ func (v *connection) establishSocketConnection() (net.Conn, error) {
r := rand.New(rand.NewSource(time.Now().Unix()))
for _, j := range r.Perm(len(ips)) {
// j comes from random permutation of indexes - ips[j] will access a random resolved ip
ip := net.IP.String(ips[j])
if strings.HasPrefix(ip, "::") {
//handle IPV6 shorthand
ip = "[" + ip + "]"
}
addrString := ip + ":" + string(port)
addrString := net.JoinHostPort(ips[j].String(), port) // IPv6 returns "[host]:port"
conn, err := net.Dial("tcp", addrString)

if err != nil {
err_msg += fmt.Sprintf("\n '%s': %s", v.connHostsList[i], err.Error())
} else {
Expand Down

0 comments on commit a9e0685

Please sign in to comment.