Repository navigation
feat(proxy): support dynamic max_retries, retry_backoff hook, and session attempt tracking - #1096
Open
afnan-mojumder wants to merge 1 commit into
Open
afnan-mojumder wants to merge 1 commit into
afnan-mojumder wants to merge 1 commit into
Conversation
…sion attempt tracking Fixes cloudflare#466 Adds support for dynamic per-request retry configuration and non-blocking asynchronous backoff in Pingora proxies: 1. `Session::tries()` and `Session::retry_attempts()`: Expose the upstream attempt and retry counts on `Session`, allowing filters and peer selection logic to inspect the current attempt index without needing custom context counters. 2. `ProxyHttp::max_retries()`: Allows overriding `ServerConf::max_retries` dynamically per request/route. 3. `ProxyHttp::retry_backoff()`: Dedicated async hook called prior to each upstream retry attempt, permitting non-blocking asynchronous sleep (such as exponential backoff) and early retry cancellation (e.g. when an elapsed time budget is reached). 4. Updated `examples/backoff_retry.rs` to demonstrate the new idiomatic hooks. 5. Added 5 unit tests verifying attempt counting, dynamic retry caps, early cancellation, and fallback to server configuration.
afnan-mojumder
force-pushed
the
feat-configurable-retries-and-backoff
branch
from
November 14, 2026 07:19
acf4755 to
1b23b01
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #466
Summary
While Pingora allows configuring an upper bound on retries globally via
conf.max_retries, proxy operators currently face three major limitations when implementing retry and backoff strategies:HttpProxy::process_request, theretriescounter was maintained only as a local stack variable. NeitherSessionnor user filters (upstream_peer,fail_to_connect,error_while_proxy,logging, etc.) could inspect which attempt was currently in progress. As noted in Make MAX_RETRIES configurable #466 and community discussions, users had to manually invent and increment custom counter fields across filters, which breaks whenever an error occurs outsidefail_to_connect.max_retriescould not be configured per-request or per-route based on path, headers, or client identity.fail_to_connectis synchronous, users wanting a delay before retrying resorted tostd::thread::sleep, which blocks Tokio worker threads and harms proxy concurrency. Inexamples/backoff_retry.rs, sleep was temporarily hacked intoupstream_peer.This pull request introduces clean, non-breaking abstractions to resolve all three issues.
Changes
Attempt & Retry Tracking on
Session:Session::tries(&self) -> usize: Returns 0 before upstream attempts begin, 1 on the initial attempt, and increments on each retry.Session::retry_attempts(&self) -> usize: Returns 0 on the initial attempt, 1 on the first retry, etc. (self.tries.saturating_sub(1)).session.Per-Request
max_retriesHook:ProxyHttp::max_retries(&self, session: &Session<DS>, ctx: &Self::CTX) -> Option<usize>:Noneby default to preserve existing behavior (falling back toconf.max_retries).Some(n)allows setting per-request limits (e.g.Some(1)to disallow retries on specific routes, or higher limits for cold-starting backends).Asynchronous
retry_backoffHook:ProxyHttp::retry_backoff(&self, session: &mut Session<DS>, ctx: &mut Self::CTX, error: &Error) -> bool:tokio::time::sleep).false.trueby default (no-op).Updated Example & Documentation:
pingora-proxy/examples/backoff_retry.rsto show the idiomatic use ofretry_backoff,max_retries, andsession.retry_attempts().Unit Tests:
mod retry_configuration_and_backoff:session_tries_and_retry_attempts_initialmax_retries_override_and_retry_backoff_calledmax_retries_override_allows_single_attemptretry_backoff_can_abort_retry_earlymax_retries_falls_back_to_server_confVerification
cargo check -p pingora-proxy: PASScargo clippy --all-targets -p pingora-proxy -- -D warnings: PASS (0 warnings)cargo fmt --all -- --check: PASScargo test -p pingora-proxy --lib: PASS (all 227 unit tests pass)cargo build --example backoff_retry: PASS