1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-07-02 16:38:36 +00:00

allow for different timeframes for configuration

This commit is contained in:
Matthias Ahouansou 2024-06-26 13:36:04 +01:00
parent bdf12c2bbd
commit 024f910bf9
No known key found for this signature in database
3 changed files with 32 additions and 7 deletions

View file

@ -115,10 +115,30 @@ pub enum Restriction {
CatchAll,
}
#[derive(Deserialize, Clone, Copy, Debug)]
#[serde(rename_all = "snake_case")]
pub enum Timeframe {
PerSecond(NonZeroU64),
PerMinute(NonZeroU64),
PerHour(NonZeroU64),
PerDay(NonZeroU64),
}
impl Timeframe {
pub fn nano_gap(&self) -> u64 {
match self {
Timeframe::PerSecond(t) => 1000 * 1000 * 1000 / t.get(),
Timeframe::PerMinute(t) => 1000 * 1000 * 1000 * 60 / t.get(),
Timeframe::PerHour(t) => 1000 * 1000 * 1000 * 60 * 60 / t.get(),
Timeframe::PerDay(t) => 1000 * 1000 * 1000 * 60 * 60 * 24 / t.get(),
}
}
}
#[derive(Clone, Copy, Debug, Deserialize)]
pub struct Limitation {
#[serde(default = "default_non_zero")]
pub per_minute: NonZeroU64,
#[serde(default = "default_non_zero", flatten)]
pub timeframe: Timeframe,
#[serde(default = "default_non_zero")]
pub burst_capacity: NonZeroU64,
#[serde(default = "default_non_zero")]
@ -340,7 +360,7 @@ pub fn default_rate_limit() -> BTreeMap<Restriction, Limitation> {
BTreeMap::from_iter([(
Restriction::default(),
Limitation {
per_minute: default_non_zero(),
timeframe: Timeframe::PerMinute(default_non_zero()),
burst_capacity: default_non_zero(),
weight: default_non_zero(),
},