Skip to content

Don't start backoff timer if it would exceed context deadline#284

Open
c-tonneslan wants to merge 1 commit into
hashicorp:mainfrom
c-tonneslan:fix/backoff-deadline-check
Open

Don't start backoff timer if it would exceed context deadline#284
c-tonneslan wants to merge 1 commit into
hashicorp:mainfrom
c-tonneslan:fix/backoff-deadline-check

Conversation

@c-tonneslan
Copy link
Copy Markdown

If the calculated backoff wait would push past the context deadline, we're just going to block until the deadline fires and get context.DeadlineExceeded anyway. This change checks for that upfront and returns immediately instead of sleeping unnecessarily.

The fix is a single deadline check before time.NewTimer:

if deadline, ok := req.Context().Deadline(); ok && time.Now().Add(wait).After(deadline) {
    c.HTTPClient.CloseIdleConnections()
    return nil, req.Context().Err()
}

This makes callers with tight deadlines get the error back faster, without the full backoff sleep eating into their remaining budget.

Existing tests pass (go test ./...).

Fixes #272

If the backoff wait time would push past the context deadline, there's no
point starting the timer at all. We'd just block until the deadline fires
and get context.DeadlineExceeded anyway. Instead, check upfront and return
immediately so callers get the error without the unnecessary wait.
@c-tonneslan c-tonneslan requested a review from a team as a code owner May 10, 2026 18:04
@hashicorp-cla-app
Copy link
Copy Markdown

hashicorp-cla-app Bot commented May 10, 2026

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backoff wait sleep should check that it wouldn't exceed the context Deadline

1 participant