Use the reqwest::retry::Builder to define how the Client should handle retries. A retry policy consists of a scope (which requests are eligible), a classifier (which specific results trigger a retry), a retry budget (to prevent retry storms), and a per-request limit.
Key Concepts
- Scope: Policies are scoped. A policy only applies to requests that fall within its defined scope (e.g., a specific host). This prevents a single retry budget from being exhausted by unrelated requests.
- Classifier: Determines if a specific request/response pair is
Retryable or a Success. Warning: Only retry requests that are idempotent or safe to execute multiple times. - Retry Budget: Controls the total amount of extra load retries can add. By default, policies include a budget that permits 20% extra requests. Disabling this is not recommended as it can lead to retry storms.
- Max Retries per Request: Limits how many times a single logical request is retried, regardless of the overall budget.
// Example of creating a builder with a custom classifier
let builder = reqwest::retry::Builder::for_host("api.example.com")
.classify_fn(|req_rep| {
match (req_rep.method(), req_rep.status()) {
(&http::Method::GET, Some(http::StatusCode::SERVICE_UNAVAILABLE)) => {
req_rep.retryable()
},
_ => req_rep.success()
}
});