chorus/src/limit.rs

52 lines
1.9 KiB
Rust
Raw Normal View History

use crate::api::limits::{LimitType, Limits};
use reqwest::{Client, RequestBuilder, Response};
2023-04-07 21:51:50 +02:00
use std::collections::VecDeque;
2023-04-08 14:51:36 +02:00
// Note: There seem to be some overlapping request limiters. We need to make sure that sending a
// request checks for all the request limiters that apply, and blocks if any of the limiters are 0
#[allow(dead_code)]
2023-04-07 21:51:50 +02:00
pub struct LimitedRequester {
http: Client,
requests: VecDeque<RequestBuilder>,
last_reset_epoch: i64,
2023-04-12 16:49:18 +02:00
limits_rate: Limits,
}
2023-04-07 21:51:50 +02:00
impl LimitedRequester {
/// Create a new `LimitedRequester`. `LimitedRequester`s use a `VecDeque` to store requests and
/// send them to the server using a `Client`. It keeps track of the remaining requests that can
/// be send within the `Limit` of an external API Ratelimiter, and looks at the returned request
/// headers to see if it can find Ratelimit info to update itself.
#[allow(dead_code)]
2023-04-08 14:51:36 +02:00
pub async fn new(api_url: String) -> Self {
2023-04-07 21:51:50 +02:00
LimitedRequester {
http: Client::new(),
2023-04-07 21:51:50 +02:00
requests: VecDeque::new(),
last_reset_epoch: chrono::Utc::now().timestamp(),
2023-04-12 16:49:18 +02:00
limits_rate: Limits::check_limits(api_url).await,
}
}
fn add_to_queue(request: RequestBuilder, queue: &mut VecDeque<RequestBuilder>) {
queue.push_back(request);
}
2023-04-12 18:34:20 +02:00
fn update_limits(&mut self, response: Response, limit_types: Vec<LimitType>) {
// TODO: Make this work {
let remaining = match response.headers().get("X-RateLimit-Remaining") {
Some(remaining) => remaining,
2023-04-12 18:34:20 +02:00
None => return, //false,
};
let limit = match response.headers().get("X-RateLimit-Limit") {
Some(limit) => limit,
2023-04-12 18:34:20 +02:00
None => return, //false,
};
let reset = match response.headers().get("X-RateLimit-Reset") {
Some(reset) => reset,
2023-04-12 18:34:20 +02:00
None => return, //false,
};
}
}