Skip to content

Commit d03b7fa

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

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

internal/pool/pool.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,29 +319,24 @@ 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
}
336338
}
337339

338-
netConn, err := p.cfg.Dialer(ctx)
339-
if err != nil {
340-
lastErr = err
341-
// Continue to next retry attempt
342-
continue
343-
}
344-
345340
// Success - create connection
346341
cn := NewConnWithBufferSize(netConn, p.cfg.ReadBufferSize, p.cfg.WriteBufferSize)
347342
cn.pooled = pooled

0 commit comments

Comments
 (0)