This commit is contained in:
singh-bhawani 2024-11-21 10:45:58 +08:00 committed by GitHub
commit 3b44be7fa3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 29 additions and 0 deletions

View File

@ -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