Compare commits

...

2 Commits

Author SHA1 Message Date
singh-bhawani 4cccc58dfd
Merge 711df7ff8d into 930d904205 2024-11-19 00:14:58 +03:00
Bhawani Singh 711df7ff8d added readFromAny(master+replica) for better resource utilization 2024-05-13 13:43:06 +05:30
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.
ReplicaOnly bool
//Route all read-only commands to master + replica nodes.
ReadFromAny bool
// Use replicas disconnected with master when cannot get connected replicas
// Now, this option only works in RandomReplicaAddr function.
UseDisconnectedReplicas bool
@ -262,6 +265,8 @@ func masterReplicaDialer(
if failover.opt.ReplicaOnly {
addr, err = failover.RandomReplicaAddr(ctx)
} else if failover.opt.ReadFromAny {
addr, err = failover.RandomAddr(ctx)
} else {
addr, err = failover.MasterAddr(ctx)
if err == nil {
@ -512,6 +517,30 @@ func (c *sentinelFailover) RandomReplicaAddr(ctx context.Context) (string, error
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) {
c.mu.RLock()
sentinel := c.sentinel