Skip to content

Commit b64d9de

Browse files
daviddzxymonkey92t
andauthored
Handle IPv6 in isMovedError (#2981)
* Handle IPv6 in isMovedError * Simplify GetAddr --------- Co-authored-by: Monkey <golang@88.com>
1 parent fa9edec commit b64d9de

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

error.go

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

+17
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,19 @@ func ReplaceSpaces(s string) string {
6465

6566
return builder.String()
6667
}
68+
69+
func GetAddr(addr string) string {
70+
ind := strings.LastIndexByte(addr, ':')
71+
if ind == -1 {
72+
return ""
73+
}
74+
75+
if strings.IndexByte(addr, '.') != -1 {
76+
return addr
77+
}
78+
79+
if addr[0] == '[' {
80+
return addr
81+
}
82+
return net.JoinHostPort(addr[:ind], addr[ind+1:])
83+
}

internal/util_test.go

+21
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)