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.
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.
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:
- Checks the cache: If the request is already cached, it bypasses 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
- If the request is already cached, the middleware immediately forwards it.
- If throttling is required, it waits according to the configured delay.
- If a request fails, exponential backoff is applied before retrying.