pub struct DriveThrottleBackoff { /* private fields */ }Expand description
Implements a throttling and exponential backoff middleware for HTTP requests.
This middleware limits request concurrency and applies adaptive delays between retries, helping to prevent rate-limiting issues when interacting with APIs that enforce request quotas.
It can run in two modes:
- Cache-aware mode via
DriveThrottleBackoff::new, where cached requests can bypass throttling. - Throttle-only mode via
DriveThrottleBackoff::without_cache, where all requests are throttled.
Requests are throttled using a semaphore-based approach, ensuring that
the maximum number of concurrent requests does not exceed max_concurrent.
If a request fails, it enters a retry loop where each retry is delayed according to an exponential backoff strategy.
Implementations§
Source§impl DriveThrottleBackoff
impl DriveThrottleBackoff
Sourcepub fn new(policy: ThrottlePolicy, cache: Arc<DriveCache>) -> Self
pub fn new(policy: ThrottlePolicy, cache: Arc<DriveCache>) -> Self
Creates a new DriveThrottleBackoff middleware with the specified throttling policy.
§Arguments
policy- The throttling configuration defining concurrency limits, delays, and retry behavior.cache- The shared cache instance used for detecting previously cached requests.
§Returns
A new instance of DriveThrottleBackoff.
Sourcepub fn without_cache(policy: ThrottlePolicy) -> Self
pub fn without_cache(policy: ThrottlePolicy) -> Self
Creates a new DriveThrottleBackoff middleware without any cache integration.
In this mode, every request is throttled based on the configured policy, and no cache checks are performed.
pub fn available_permits(&self) -> usize
Trait Implementations§
Source§impl Middleware for DriveThrottleBackoff
impl Middleware for DriveThrottleBackoff
Source§fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Handles throttling and retry logic for HTTP requests.
This method:
- Optionally checks the cache: In cache-aware mode, cached requests bypass throttling.
- Enforces concurrency limits: Ensures no more than
max_concurrentrequests are in flight. - Applies an initial delay before sending the request.
- Retries failed requests: Uses exponential backoff with jitter for failed requests.
§Arguments
req- The incoming HTTP request.extensions- A mutable reference to request extensions, used for tracking metadata.next- The next middleware in the request chain.
§Returns
A Result<Response, Error> containing either:
- A successfully processed response.
- An error if the request failed after exhausting all retries.
§Behavior
- In cache-aware mode, if the request is already cached, the middleware immediately forwards it.
- In throttle-only mode, cache checks are skipped.
- If throttling is required, it waits according to the configured delay.
- If a request fails, exponential backoff is applied before retrying.