@@ -570,6 +570,7 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
570
570
masterAddr string
571
571
wg sync.WaitGroup
572
572
once sync.Once
573
+ errCh = make (chan error , len (c .sentinelAddrs ))
573
574
)
574
575
575
576
for i , sentinelAddr := range c .sentinelAddrs {
@@ -579,6 +580,11 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
579
580
sentinelCli := NewSentinelClient (c .opt .sentinelOptions (addr ))
580
581
addrVal , err := sentinelCli .GetMasterAddrByName (ctx , c .opt .MasterName ).Result ()
581
582
if err != nil {
583
+ if errors .Is (err , context .Canceled ) || errors .Is (err , context .DeadlineExceeded ) {
584
+ // Report immediately and return
585
+ errCh <- err
586
+ return
587
+ }
582
588
internal .Logger .Printf (ctx , "sentinel: GetMasterAddrByName addr=%s, master=%q failed: %s" ,
583
589
addr , c .opt .MasterName , err )
584
590
_ = sentinelCli .Close ()
@@ -595,12 +601,21 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
595
601
}(i , sentinelAddr )
596
602
}
597
603
598
- wg .Wait ()
604
+ done := make (chan struct {})
605
+ go func () {
606
+ wg .Wait ()
607
+ close (done )
608
+ }()
599
609
600
- if masterAddr != "" {
601
- return masterAddr , nil
610
+ select {
611
+ case <- done :
612
+ if masterAddr != "" {
613
+ return masterAddr , nil
614
+ }
615
+ return "" , errors .New ("redis: all sentinels specified in configuration are unreachable" )
616
+ case err := <- errCh :
617
+ return "" , err
602
618
}
603
- return "" , errors .New ("redis: all sentinels specified in configuration are unreachable" )
604
619
}
605
620
606
621
func (c * sentinelFailover ) replicaAddrs (ctx context.Context , useDisconnected bool ) ([]string , error ) {
0 commit comments