From bff611bc4181187db83f90d6474607f8f3f15ffa Mon Sep 17 00:00:00 2001 From: YenchangChan Date: Fri, 7 Jun 2024 10:01:37 +0800 Subject: [PATCH] feat: random idx of replicas when init cluster connection --- pool/conn.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pool/conn.go b/pool/conn.go index d58d4b81..909a848c 100644 --- a/pool/conn.go +++ b/pool/conn.go @@ -22,6 +22,7 @@ import ( "context" "crypto/tls" "fmt" + "math/rand" "sync" "time" @@ -181,7 +182,10 @@ func InitClusterConn(chCfg *config.ClickHouseConfig) (err error) { sc.opts.ConnMaxLifetime = time.Minute * 10 } sc.protocol = proto - if _, _, err = sc.NextGoodReplica(0); err != nil { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + idx := r.Intn(numReplicas) + sc.nextRep = idx + if _, _, err = sc.NextGoodReplica(idx); err != nil { return } clusterConn = append(clusterConn, sc)