Skip to main content

run_with_retry

Function run_with_retry 

Source
pub async fn run_with_retry<F, Fut, T>(
    keys: &[ApiKey],
    operation: F,
) -> Result<T>
where F: Fn(String) -> Fut, Fut: Future<Output = Result<T, Context7Error>>,
Expand description

Executes an API call with retry and key rotation.

Shuffles a local copy of the provided keys (random draw without replacement) and retries up to min(keys.len(), 5) times with exponential backoff: 500 ms → 1 s → 2 s.

Short-circuits immediately on parse errors (status 200 but JSON failed) — retrying with another key would not help in that case.

The closure receives an owned String (clone of the key) to satisfy the async move ownership requirement inside the future.