Skip to content

Commit 6d78214

Browse files
authored
Stop goroutines early after success is achieved
1 parent d231fa0 commit 6d78214

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

sentinel.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
573573
errCh = make(chan error, len(c.sentinelAddrs))
574574
)
575575

576+
ctx, cancel := context.WithCancel(ctx)
577+
defer cancel()
578+
576579
for i, sentinelAddr := range c.sentinelAddrs {
577580
wg.Add(1)
578581
go func(i int, addr string) {
@@ -597,6 +600,7 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
597600
c.sentinelAddrs[0], c.sentinelAddrs[i] = c.sentinelAddrs[i], c.sentinelAddrs[0]
598601
c.setSentinel(ctx, sentinelCli)
599602
internal.Logger.Printf(ctx, "sentinel: selected addr=%s masterAddr=%s", addr, masterAddr)
603+
cancel()
600604
})
601605
}(i, sentinelAddr)
602606
}

0 commit comments

Comments
 (0)