@@ -570,6 +570,7 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
570570 masterAddr string
571571 wg sync.WaitGroup
572572 once sync.Once
573+ errCh = make (chan error , len (c .sentinelAddrs ))
573574 )
574575
575576 for i , sentinelAddr := range c .sentinelAddrs {
@@ -579,6 +580,11 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
579580 sentinelCli := NewSentinelClient (c .opt .sentinelOptions (addr ))
580581 addrVal , err := sentinelCli .GetMasterAddrByName (ctx , c .opt .MasterName ).Result ()
581582 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+ }
582588 internal .Logger .Printf (ctx , "sentinel: GetMasterAddrByName addr=%s, master=%q failed: %s" ,
583589 addr , c .opt .MasterName , err )
584590 _ = sentinelCli .Close ()
@@ -595,12 +601,21 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
595601 }(i , sentinelAddr )
596602 }
597603
598- wg .Wait ()
604+ done := make (chan struct {})
605+ go func () {
606+ wg .Wait ()
607+ close (done )
608+ }()
599609
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
602618 }
603- return "" , errors .New ("redis: all sentinels specified in configuration are unreachable" )
604619}
605620
606621func (c * sentinelFailover ) replicaAddrs (ctx context.Context , useDisconnected bool ) ([]string , error ) {
0 commit comments