Skip to content

Commit

Permalink
feat: unique redis addrs and trim spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Mar 10, 2023
1 parent f8b2dc8 commit 5d337ac
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
3 changes: 1 addition & 2 deletions core/stores/redis/redisblockingnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package redis

import (
"fmt"
"strings"

red "github.com/go-redis/redis/v8"
"github.com/zeromicro/go-zero/core/logx"
Expand Down Expand Up @@ -32,7 +31,7 @@ func CreateBlockingNode(r *Redis) (ClosableNode, error) {
return &clientBridge{client}, nil
case ClusterType:
client := red.NewClusterClient(&red.ClusterOptions{
Addrs: strings.Split(r.Addr, ","),
Addrs: splitClusterAddrs(r.Addr),
Password: r.Pass,
MaxRetries: maxRetries,
PoolSize: 1,
Expand Down
19 changes: 18 additions & 1 deletion core/stores/redis/redisclustermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/zeromicro/go-zero/core/syncx"
)

const addrSep = ","

var clusterManager = syncx.NewResourceManager()

func getCluster(r *Redis) (*red.ClusterClient, error) {
Expand All @@ -20,7 +22,7 @@ func getCluster(r *Redis) (*red.ClusterClient, error) {
}
}
store := red.NewClusterClient(&red.ClusterOptions{
Addrs: strings.Split(r.Addr, ","),
Addrs: splitClusterAddrs(r.Addr),
Password: r.Pass,
MaxRetries: maxRetries,
MinIdleConns: idleConns,
Expand All @@ -36,3 +38,18 @@ func getCluster(r *Redis) (*red.ClusterClient, error) {

return val.(*red.ClusterClient), nil
}

func splitClusterAddrs(addr string) []string {
addrs := strings.Split(addr, addrSep)
unique := make(map[string]struct{})
for _, each := range addrs {
unique[strings.TrimSpace(each)] = struct{}{}
}

addrs = addrs[:0]
for k := range unique {
addrs = append(addrs, k)
}

return addrs
}
43 changes: 43 additions & 0 deletions core/stores/redis/redisclustermanager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package redis

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSplitClusterAddrs(t *testing.T) {
testCases := []struct {
name string
input string
expected []string
}{
{
name: "empty input",
input: "",
expected: []string{""},
},
{
name: "single address",
input: "127.0.0.1:8000",
expected: []string{"127.0.0.1:8000"},
},
{
name: "multiple addresses with duplicates",
input: "127.0.0.1:8000,127.0.0.1:8001, 127.0.0.1:8000",
expected: []string{"127.0.0.1:8000", "127.0.0.1:8001"},
},
{
name: "multiple addresses without duplicates",
input: "127.0.0.1:8000, 127.0.0.1:8001, 127.0.0.1:8002",
expected: []string{"127.0.0.1:8000", "127.0.0.1:8001", "127.0.0.1:8002"},
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.ElementsMatch(t, tc.expected, splitClusterAddrs(tc.input))
})
}
}

0 comments on commit 5d337ac

Please sign in to comment.