Skip to content

Commit 22e1d74

Browse files
committed
fix break of in dial
1 parent b65bc6e commit 22e1d74

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

internal/pool/pool.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -319,26 +319,22 @@ func (p *ConnPool) dialConn(ctx context.Context, pooled bool) (*Conn, error) {
319319
const backoffDuration = 100 * time.Millisecond
320320

321321
var lastErr error
322-
for attempt := 0; attempt < maxRetries; attempt++ {
323-
// Add backoff delay for retry attempts
324-
// (not for the first attempt, do at least one)
325-
if attempt > 0 {
322+
shouldLoop := true
323+
// when the timeout is reached, we should stop retrying
324+
// but keep the lastErr to return to the caller
325+
// instead of a generic context deadline exceeded error
326+
for attempt := 0; (attempt < maxRetries) && shouldLoop; attempt++ {
327+
netConn, err := p.cfg.Dialer(ctx)
328+
if err != nil {
329+
lastErr = err
330+
// Add backoff delay for retry attempts
331+
// (not for the first attempt, do at least one)
326332
select {
327333
case <-ctx.Done():
328-
// we should have lastErr set, but just in case
329-
if lastErr == nil {
330-
lastErr = ctx.Err()
331-
}
332-
break
334+
shouldLoop = false
333335
case <-time.After(backoffDuration):
334336
// Continue with retry
335337
}
336-
}
337-
338-
netConn, err := p.cfg.Dialer(ctx)
339-
if err != nil {
340-
lastErr = err
341-
// Continue to next retry attempt
342338
continue
343339
}
344340

0 commit comments

Comments
 (0)