From 1dfab7747297e1a7723089375c1399f607b640a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Bal=C3=A1=C5=BE?= Date: Wed, 24 Apr 2024 22:30:39 +0200 Subject: [PATCH 1/2] Handle IPv6 in isMovedError --- error.go | 3 +++ internal/util.go | 20 ++++++++++++++++++++ internal/util_test.go | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/error.go b/error.go index 8a59913be..9b348193a 100644 --- a/error.go +++ b/error.go @@ -7,6 +7,7 @@ import ( "net" "strings" + "github.com/redis/go-redis/v9/internal" "github.com/redis/go-redis/v9/internal/pool" "github.com/redis/go-redis/v9/internal/proto" ) @@ -129,7 +130,9 @@ func isMovedError(err error) (moved bool, ask bool, addr string) { if ind == -1 { return false, false, "" } + addr = s[ind+1:] + addr = internal.GetAddr(addr) return } diff --git a/internal/util.go b/internal/util.go index ed81ad7aa..ee96d73c4 100644 --- a/internal/util.go +++ b/internal/util.go @@ -2,6 +2,7 @@ package internal import ( "context" + "net" "strings" "time" @@ -64,3 +65,22 @@ func ReplaceSpaces(s string) string { return builder.String() } + +func GetAddr(addr string) string { + ind := strings.LastIndex(addr, ":") + if ind == -1 { + return "" + } + + port := addr[ind+1:] + host := addr[:ind] + if string(host[0]) == "[" && string(host[len(host)-1]) == "]" { + host = host[1 : len(host)-1] + } + + if net.ParseIP(host) == nil { + return "" + } + + return net.JoinHostPort(host, port) +} diff --git a/internal/util_test.go b/internal/util_test.go index f090ebaa4..57f7f9fa1 100644 --- a/internal/util_test.go +++ b/internal/util_test.go @@ -51,3 +51,24 @@ func TestIsLower(t *testing.T) { Expect(isLower(str)).To(BeTrue()) }) } + +func TestGetAddr(t *testing.T) { + It("getAddr", func() { + str := "127.0.0.1:1234" + Expect(GetAddr(str)).To(Equal(str)) + + str = "[::1]:1234" + Expect(GetAddr(str)).To(Equal(str)) + + str = "[fd01:abcd::7d03]:6379" + Expect(GetAddr(str)).To(Equal(str)) + + Expect(GetAddr("::1:1234")).To(Equal("[::1]:1234")) + + Expect(GetAddr("fd01:abcd::7d03:6379")).To(Equal("[fd01:abcd::7d03]:6379")) + + Expect(GetAddr("127.0.0.1")).To(Equal("")) + + Expect(GetAddr("127")).To(Equal("")) + }) +} From 8bb17a44af9215b9d7cbf1347a42f32e84d26114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Bal=C3=A1=C5=BE?= Date: Sat, 27 Apr 2024 09:21:32 +0200 Subject: [PATCH 2/2] Simplify GetAddr --- internal/util.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/internal/util.go b/internal/util.go index ee96d73c4..235a91afa 100644 --- a/internal/util.go +++ b/internal/util.go @@ -67,20 +67,17 @@ func ReplaceSpaces(s string) string { } func GetAddr(addr string) string { - ind := strings.LastIndex(addr, ":") + ind := strings.LastIndexByte(addr, ':') if ind == -1 { return "" } - port := addr[ind+1:] - host := addr[:ind] - if string(host[0]) == "[" && string(host[len(host)-1]) == "]" { - host = host[1 : len(host)-1] + if strings.IndexByte(addr, '.') != -1 { + return addr } - if net.ParseIP(host) == nil { - return "" + if addr[0] == '[' { + return addr } - - return net.JoinHostPort(host, port) + return net.JoinHostPort(addr[:ind], addr[ind+1:]) }