Handle IPv6 in isMovedError (#2981)

* Handle IPv6 in isMovedError

* Simplify GetAddr

---------

Co-authored-by: Monkey <golang@88.com>
This commit is contained in:
Dávid Baláž 2024-04-28 06:37:44 +02:00 committed by GitHub
parent fa9edecebc
commit b64d9deef3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 0 deletions

View File

@ -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
}

View File

@ -2,6 +2,7 @@ package internal
import (
"context"
"net"
"strings"
"time"
@ -64,3 +65,19 @@ func ReplaceSpaces(s string) string {
return builder.String()
}
func GetAddr(addr string) string {
ind := strings.LastIndexByte(addr, ':')
if ind == -1 {
return ""
}
if strings.IndexByte(addr, '.') != -1 {
return addr
}
if addr[0] == '[' {
return addr
}
return net.JoinHostPort(addr[:ind], addr[ind+1:])
}

View File

@ -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(""))
})
}