@@ -45,6 +45,9 @@ type FailoverOptions struct {
45
45
// Route all commands to replica read-only nodes.
46
46
ReplicaOnly bool
47
47
48
+ //Route all read-only commands to master + replica nodes.
49
+ ReadFromAny bool
50
+
48
51
// Use replicas disconnected with master when cannot get connected replicas
49
52
// Now, this option only works in RandomReplicaAddr function.
50
53
UseDisconnectedReplicas bool
@@ -259,6 +262,8 @@ func masterReplicaDialer(
259
262
260
263
if failover .opt .ReplicaOnly {
261
264
addr , err = failover .RandomReplicaAddr (ctx )
265
+ } else if failover .opt .ReadFromAny {
266
+ addr , err = failover .RandomAddr (ctx )
262
267
} else {
263
268
addr , err = failover .MasterAddr (ctx )
264
269
if err == nil {
@@ -509,6 +514,30 @@ func (c *sentinelFailover) RandomReplicaAddr(ctx context.Context) (string, error
509
514
return addresses [rand .Intn (len (addresses ))], nil
510
515
}
511
516
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
+
512
541
func (c * sentinelFailover ) MasterAddr (ctx context.Context ) (string , error ) {
513
542
c .mu .RLock ()
514
543
sentinel := c .sentinel
0 commit comments