File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -815,6 +815,18 @@ func NewFailoverClusterClient(failoverOpt *FailoverOptions) *ClusterClient {
815
815
}
816
816
817
817
opt := failoverOpt .clusterOptions ()
818
+ if failoverOpt .DB != 0 {
819
+ onConnect := opt .OnConnect
820
+
821
+ opt .OnConnect = func (ctx context.Context , cn * Conn ) error {
822
+ if err := cn .Select (ctx , failoverOpt .DB ).Err (); err != nil {
823
+ return err
824
+ }
825
+
826
+ return onConnect (ctx , cn )
827
+ }
828
+ }
829
+
818
830
opt .ClusterSlots = func (ctx context.Context ) ([]ClusterSlot , error ) {
819
831
masterAddr , err := failover .MasterAddr (ctx )
820
832
if err != nil {
Original file line number Diff line number Diff line change @@ -200,6 +200,7 @@ var _ = Describe("NewFailoverClusterClient", func() {
200
200
SentinelAddrs : sentinelAddrs ,
201
201
202
202
RouteRandomly : true ,
203
+ DB : 1 ,
203
204
})
204
205
Expect (client .FlushDB (ctx ).Err ()).NotTo (HaveOccurred ())
205
206
@@ -289,6 +290,20 @@ var _ = Describe("NewFailoverClusterClient", func() {
289
290
})
290
291
})
291
292
293
+ It ("should sentinel cluster client db" , func () {
294
+ err := client .ForEachShard (ctx , func (ctx context.Context , c * redis.Client ) error {
295
+ return c .Ping (ctx ).Err ()
296
+ })
297
+ Expect (err ).NotTo (HaveOccurred ())
298
+
299
+ _ = client .ForEachShard (ctx , func (ctx context.Context , c * redis.Client ) error {
300
+ clientInfo , err := c .ClientInfo (ctx ).Result ()
301
+ Expect (err ).NotTo (HaveOccurred ())
302
+ Expect (clientInfo .DB ).To (Equal (1 ))
303
+ return nil
304
+ })
305
+ })
306
+
292
307
It ("should sentinel cluster PROTO 3" , func () {
293
308
_ = client .ForEachShard (ctx , func (ctx context.Context , c * redis.Client ) error {
294
309
val , err := client .Do (ctx , "HELLO" ).Result ()
You can’t perform that action at this time.
0 commit comments