mirror of
https://github.com/redis/go-redis.git
synced 2025-04-16 09:23:06 +03:00
Use DB option in NewFailoverClusterClient (#3342)
This commit is contained in:
parent
93bc3e6140
commit
eedb171825
16
sentinel.go
16
sentinel.go
@ -815,6 +815,22 @@ func NewFailoverClusterClient(failoverOpt *FailoverOptions) *ClusterClient {
|
||||
}
|
||||
|
||||
opt := failoverOpt.clusterOptions()
|
||||
if failoverOpt.DB != 0 {
|
||||
onConnect := opt.OnConnect
|
||||
|
||||
opt.OnConnect = func(ctx context.Context, cn *Conn) error {
|
||||
if err := cn.Select(ctx, failoverOpt.DB).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if onConnect != nil {
|
||||
return onConnect(ctx, cn)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
opt.ClusterSlots = func(ctx context.Context) ([]ClusterSlot, error) {
|
||||
masterAddr, err := failover.MasterAddr(ctx)
|
||||
if err != nil {
|
||||
|
@ -200,6 +200,7 @@ var _ = Describe("NewFailoverClusterClient", func() {
|
||||
SentinelAddrs: sentinelAddrs,
|
||||
|
||||
RouteRandomly: true,
|
||||
DB: 1,
|
||||
})
|
||||
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
|
||||
|
||||
@ -289,6 +290,20 @@ var _ = Describe("NewFailoverClusterClient", func() {
|
||||
})
|
||||
})
|
||||
|
||||
It("should sentinel cluster client db", func() {
|
||||
err := client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
|
||||
return c.Ping(ctx).Err()
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_ = client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
|
||||
clientInfo, err := c.ClientInfo(ctx).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(clientInfo.DB).To(Equal(1))
|
||||
return nil
|
||||
})
|
||||
})
|
||||
|
||||
It("should sentinel cluster PROTO 3", func() {
|
||||
_ = client.ForEachShard(ctx, func(ctx context.Context, c *redis.Client) error {
|
||||
val, err := client.Do(ctx, "HELLO").Result()
|
||||
|
Loading…
x
Reference in New Issue
Block a user