Repository: zeromicro/go-zero
Pull request #3004 (2023-03-10 07:41:43)

feat: unique redis addrs and trim spaces

codecov[bot]: ## [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/3004?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3004](https://codecov.io/gh/zeromicro/go-zero/pull/3004?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (5d337ac) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/3c9fe0b381b3c0c1e0e3d53d3cdbe2cd96061a43?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (3c9fe0b) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head 5d337ac differs from pull request most recent head 4a232ab. Consider uploading reports for the commit 4a232ab to get more accurate results


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/3004?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/stores/redis/redisblockingnode.go](https://codecov.io/gh/zeromicro/go-zero/pull/3004?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvcmVkaXMvcmVkaXNibG9ja2luZ25vZGUuZ28=) | `91.42% <100.00%> (ø)` | |
| [core/stores/redis/redisclustermanager.go](https://codecov.io/gh/zeromicro/go-zero/pull/3004?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvcmVkaXMvcmVkaXNjbHVzdGVybWFuYWdlci5nbw==) | `75.75% <100.00%> (+12.12%)` | :arrow_up: |


Patch:
diff --git a/core/stores/redis/redisblockingnode.go b/core/stores/redis/redisblockingnode.go
index bdb47871..106df626 100644
--- a/core/stores/redis/redisblockingnode.go
+++ b/core/stores/redis/redisblockingnode.go
@@ -2,7 +2,6 @@ package redis
 
 import (
 	"fmt"
-	"strings"
 
 	red "github.com/go-redis/redis/v8"
 	"github.com/zeromicro/go-zero/core/logx"
@@ -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,
diff --git a/core/stores/redis/redisclustermanager.go b/core/stores/redis/redisclustermanager.go
index 1f1e1b19..dda1bcdb 100644
--- a/core/stores/redis/redisclustermanager.go
+++ b/core/stores/redis/redisclustermanager.go
@@ -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) {
@@ -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,
@@ -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
+}


Test patch:
diff --git a/core/stores/redis/redisclustermanager_test.go b/core/stores/redis/redisclustermanager_test.go
new file mode 100644
index 00000000..5782ce22
--- /dev/null
+++ b/core/stores/redis/redisclustermanager_test.go
@@ -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))
+		})
+	}
+}