@@ -45,6 +45,9 @@ type FailoverOptions struct {
4545 // Route all commands to replica read-only nodes.
4646 ReplicaOnly bool
4747
48+ //Route all read-only commands to master + replica nodes.
49+ ReadFromAny bool
50+
4851 // Use replicas disconnected with master when cannot get connected replicas
4952 // Now, this option only works in RandomReplicaAddr function.
5053 UseDisconnectedReplicas bool
@@ -259,6 +262,8 @@ func masterReplicaDialer(
259262
260263 if failover .opt .ReplicaOnly {
261264 addr , err = failover .RandomReplicaAddr (ctx )
265+ } else if failover .opt .ReadFromAny {
266+ addr , err = failover .RandomAddr (ctx )
262267 } else {
263268 addr , err = failover .MasterAddr (ctx )
264269 if err == nil {
@@ -509,6 +514,30 @@ func (c *sentinelFailover) RandomReplicaAddr(ctx context.Context) (string, error
509514 return addresses [rand .Intn (len (addresses ))], nil
510515}
511516
517+ func (c * sentinelFailover ) RandomAddr (ctx context.Context ) (string , error ) {
518+ if c .opt == nil {
519+ return "" , errors .New ("opt is nil" )
520+ }
521+
522+ addresses , err := c .replicaAddrs (ctx , false )
523+ if err != nil {
524+ return "" , err
525+ }
526+
527+ if len (addresses ) == 0 && c .opt .UseDisconnectedReplicas {
528+ addresses , err = c .replicaAddrs (ctx , true )
529+ if err != nil {
530+ return "" , err
531+ }
532+ }
533+
534+ masterAdd , _ := c .MasterAddr (ctx )
535+ addresses = append (addresses , masterAdd )
536+
537+ add := addresses [rand .Intn (len (addresses ))]
538+ return add , nil
539+ }
540+
512541func (c * sentinelFailover ) MasterAddr (ctx context.Context ) (string , error ) {
513542 c .mu .RLock ()
514543 sentinel := c .sentinel
0 commit comments