From 22e1d74d0dc9b6bb9e44cb9a343e9010aec79ffd Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Fri, 29 Aug 2025 20:05:34 +0300 Subject: [PATCH] fix break of in dial --- internal/pool/pool.go | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/internal/pool/pool.go b/internal/pool/pool.go index e2109591..f3e51c48 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -319,26 +319,22 @@ func (p *ConnPool) dialConn(ctx context.Context, pooled bool) (*Conn, error) { const backoffDuration = 100 * time.Millisecond var lastErr error - for attempt := 0; attempt < maxRetries; attempt++ { - // Add backoff delay for retry attempts - // (not for the first attempt, do at least one) - if attempt > 0 { - select { - case <-ctx.Done(): - // we should have lastErr set, but just in case - if lastErr == nil { - lastErr = ctx.Err() - } - break - case <-time.After(backoffDuration): - // Continue with retry - } - } - + shouldLoop := true + // when the timeout is reached, we should stop retrying + // but keep the lastErr to return to the caller + // instead of a generic context deadline exceeded error + for attempt := 0; (attempt < maxRetries) && shouldLoop; attempt++ { netConn, err := p.cfg.Dialer(ctx) if err != nil { lastErr = err - // Continue to next retry attempt + // Add backoff delay for retry attempts + // (not for the first attempt, do at least one) + select { + case <-ctx.Done(): + shouldLoop = false + case <-time.After(backoffDuration): + // Continue with retry + } continue }