mirror of https://github.com/go-redis/redis.git
Merge 711df7ff8d
into f1ffb55c9a
This commit is contained in:
commit
3b44be7fa3
29
sentinel.go
29
sentinel.go
|
@ -45,6 +45,9 @@ type FailoverOptions struct {
|
||||||
// Route all commands to replica read-only nodes.
|
// Route all commands to replica read-only nodes.
|
||||||
ReplicaOnly bool
|
ReplicaOnly bool
|
||||||
|
|
||||||
|
//Route all read-only commands to master + replica nodes.
|
||||||
|
ReadFromAny bool
|
||||||
|
|
||||||
// Use replicas disconnected with master when cannot get connected replicas
|
// Use replicas disconnected with master when cannot get connected replicas
|
||||||
// Now, this option only works in RandomReplicaAddr function.
|
// Now, this option only works in RandomReplicaAddr function.
|
||||||
UseDisconnectedReplicas bool
|
UseDisconnectedReplicas bool
|
||||||
|
@ -262,6 +265,8 @@ func masterReplicaDialer(
|
||||||
|
|
||||||
if failover.opt.ReplicaOnly {
|
if failover.opt.ReplicaOnly {
|
||||||
addr, err = failover.RandomReplicaAddr(ctx)
|
addr, err = failover.RandomReplicaAddr(ctx)
|
||||||
|
} else if failover.opt.ReadFromAny {
|
||||||
|
addr, err = failover.RandomAddr(ctx)
|
||||||
} else {
|
} else {
|
||||||
addr, err = failover.MasterAddr(ctx)
|
addr, err = failover.MasterAddr(ctx)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -512,6 +517,30 @@ func (c *sentinelFailover) RandomReplicaAddr(ctx context.Context) (string, error
|
||||||
return addresses[rand.Intn(len(addresses))], nil
|
return addresses[rand.Intn(len(addresses))], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *sentinelFailover) RandomAddr(ctx context.Context) (string, error) {
|
||||||
|
if c.opt == nil {
|
||||||
|
return "", errors.New("opt is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
addresses, err := c.replicaAddrs(ctx, false)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(addresses) == 0 && c.opt.UseDisconnectedReplicas {
|
||||||
|
addresses, err = c.replicaAddrs(ctx, true)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
masterAdd, _ := c.MasterAddr(ctx)
|
||||||
|
addresses = append(addresses, masterAdd)
|
||||||
|
|
||||||
|
add := addresses[rand.Intn(len(addresses))]
|
||||||
|
return add, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
|
func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
sentinel := c.sentinel
|
sentinel := c.sentinel
|
||||||
|
|
Loading…
Reference in New Issue