Async Priority Limiter
This library exports the Limiter struct. It throttles prioritised tasks by limiting the max concurrent tasks and minimum time between tasks, with up to two levels based on keys.
Basic use
Let's say I want to call an API at most once a second, one at a time, with the same priority of 10:
# block_on;
Using keys
All public methods have a variant accepting a "key". This is simply a String that referes to a second layer of limits. Say I wanted to keep sending a single request at a time, at most once every two seconds, but also limit a specific endpoint to a call once every three seconds:
# block_on;
Features
reqwest
Enables the send_limiter
and send_limited_by_key
methods for the reqwest::RequestBuilder
struct, and the update_limiter_by_retry_after_header
and update_limiter_by_key_and_retry_after_header
for the reqwest::Response
struct:
let client = reqwest::Client::new();
let limits = async_priority_limiter::Limiter::new(1);
let response_a = client
.get("herpderp")
.send_limited(&limits)
.await?
// If the status is 429 and the Retry-After-header contained a valid value, the limiter will be updated accordingly, setting a hard timeout on requests:
.update_limiter_by_retry_after_header()
.await
.text()
.await;
// Potentially limited:
let response_b = client
.get("herpderp")
.send_limited(&limits)
.await?
// If the status is 429 and the header contained a valid value, the limiter will be updated accordingly, setting a hard request stop:
.update_limiter_by_retry_after_header()
.await
.text()
.await;
open_ai
Enables the update_limiter_by_open_ai_ratelimit_headers
and update_limiter_by_open_ai_ratelimit_headers_by_key
methods of the reqwest::Response
struct.
This prevents new requests from being made if remaining tokens or requests reach zero or less until the counter resets.
Requires the reqwest
feature.