chorus/src/limit.rs

32 lines
1.1 KiB
Rust
Raw Normal View History

use crate::api::limits::Limits;
use reqwest::{Client, Request};
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<Request>,
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,
}
}
}