Skip to content

retry: add cancelable retriers that cut their waits short on shutdown - #1

Merged
nicopernas merged 1 commit into
mainfrom
nico/backoff
Sep 1, 2026
Merged

nicopernas merged 1 commit into
mainfrom
nico/backoff

Conversation

@nicopernas

Copy link
Copy Markdown
Collaborator

The retrier used one context for three jobs: the deadline for the operation,
the abort signal for the wait between attempts, and the loop's stop condition.
Callers that need the operation to run to completion had to pass a context that
is never cancelled, which also made every wait uninterruptible. Shutting down
then meant sitting out the remaining backoff interval, once per in-flight
operation.

NewCancelableConstantRetrier and NewCancelableExponentialRetrier take a second
context that governs the waits only. It never reaches the operation, so an
attempt that has begun always finishes, and the operation still runs at least
once. On stop, Retry returns the last attempt's value plus an error wrapping
stop.Err(), letting a caller tell a shutdown from a genuine failure.

The existing constructors leave the stop context nil and are unchanged: same
uninterruptible initial sleep, same WithContextTimeout semantics.

Co-Authored-By: Claude Opus 5 (1M context) noreply@anthropic.com

The retrier used one context for three jobs: the deadline for the operation,
the abort signal for the wait between attempts, and the loop's stop condition.
Callers that need the operation to run to completion had to pass a context that
is never cancelled, which also made every wait uninterruptible. Shutting down
then meant sitting out the remaining backoff interval, once per in-flight
operation.

NewCancelableConstantRetrier and NewCancelableExponentialRetrier take a second
context that governs the waits only. It never reaches the operation, so an
attempt that has begun always finishes, and the operation still runs at least
once. On stop, Retry returns the last attempt's value plus an error wrapping
stop.Err(), letting a caller tell a shutdown from a genuine failure.

The existing constructors leave the stop context nil and are unchanged: same
uninterruptible initial sleep, same WithContextTimeout semantics.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@nicopernas
nicopernas merged commit 55b0449 into main Sep 1, 2026
4 checks passed
@nicopernas
nicopernas deleted the nico/backoff branch September 1, 2026 14:45
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.

1 participant