Skip to content

Commit 1dfab77

Browse files
committed
Handle IPv6 in isMovedError
1 parent 90c7a41 commit 1dfab77

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

error.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"strings"
99

10+
"github.com/redis/go-redis/v9/internal"
1011
"github.com/redis/go-redis/v9/internal/pool"
1112
"github.com/redis/go-redis/v9/internal/proto"
1213
)
@@ -129,7 +130,9 @@ func isMovedError(err error) (moved bool, ask bool, addr string) {
129130
if ind == -1 {
130131
return false, false, ""
131132
}
133+
132134
addr = s[ind+1:]
135+
addr = internal.GetAddr(addr)
133136
return
134137
}
135138

internal/util.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"context"
5+
"net"
56
"strings"
67
"time"
78

@@ -64,3 +65,22 @@ func ReplaceSpaces(s string) string {
6465

6566
return builder.String()
6667
}
68+
69+
func GetAddr(addr string) string {
70+
ind := strings.LastIndex(addr, ":")
71+
if ind == -1 {
72+
return ""
73+
}
74+
75+
port := addr[ind+1:]
76+
host := addr[:ind]
77+
if string(host[0]) == "[" && string(host[len(host)-1]) == "]" {
78+
host = host[1 : len(host)-1]
79+
}
80+
81+
if net.ParseIP(host) == nil {
82+
return ""
83+
}
84+
85+
return net.JoinHostPort(host, port)
86+
}

internal/util_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,24 @@ func TestIsLower(t *testing.T) {
5151
Expect(isLower(str)).To(BeTrue())
5252
})
5353
}
54+
55+
func TestGetAddr(t *testing.T) {
56+
It("getAddr", func() {
57+
str := "127.0.0.1:1234"
58+
Expect(GetAddr(str)).To(Equal(str))
59+
60+
str = "[::1]:1234"
61+
Expect(GetAddr(str)).To(Equal(str))
62+
63+
str = "[fd01:abcd::7d03]:6379"
64+
Expect(GetAddr(str)).To(Equal(str))
65+
66+
Expect(GetAddr("::1:1234")).To(Equal("[::1]:1234"))
67+
68+
Expect(GetAddr("fd01:abcd::7d03:6379")).To(Equal("[fd01:abcd::7d03]:6379"))
69+
70+
Expect(GetAddr("127.0.0.1")).To(Equal(""))
71+
72+
Expect(GetAddr("127")).To(Equal(""))
73+
})
74+
}

0 commit comments

Comments
 (0)