Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle IPv6 in isMovedError #2981

Merged
merged 3 commits into from Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions error.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 17 additions & 0 deletions internal/util.go
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"context"
"net"
"strings"
"time"

Expand Down Expand Up @@ -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:])
}
21 changes: 21 additions & 0 deletions internal/util_test.go
Expand Up @@ -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(""))
})
}