mirror of
https://gitlab.com/famedly/conduit.git
synced 2025-06-27 16:35:59 +00:00
allow for different timeframes for configuration
This commit is contained in:
parent
bdf12c2bbd
commit
024f910bf9
3 changed files with 32 additions and 7 deletions
|
@ -20,7 +20,10 @@ use axum_extra::{
|
||||||
use bytes::{BufMut, BytesMut};
|
use bytes::{BufMut, BytesMut};
|
||||||
use http::{Request, StatusCode};
|
use http::{Request, StatusCode};
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::{client::error::ErrorKind, AuthScheme, IncomingRequest, OutgoingResponse},
|
api::{
|
||||||
|
client::error::{ErrorKind, RetryAfter},
|
||||||
|
AuthScheme, IncomingRequest, OutgoingResponse,
|
||||||
|
},
|
||||||
server_util::authorization::XMatrix,
|
server_util::authorization::XMatrix,
|
||||||
CanonicalJsonValue, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId, UserId,
|
CanonicalJsonValue, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId, UserId,
|
||||||
};
|
};
|
||||||
|
@ -357,14 +360,16 @@ where
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(retry_after_ms) = {
|
if let Err(retry_after) = {
|
||||||
services()
|
services()
|
||||||
.rate_limiting
|
.rate_limiting
|
||||||
.update_or_reject(target, metadata)
|
.update_or_reject(target, metadata)
|
||||||
.map_err(Some)
|
.map_err(Some)
|
||||||
} {
|
} {
|
||||||
return Err(Error::BadRequest(
|
return Err(Error::BadRequest(
|
||||||
ErrorKind::LimitExceeded { retry_after_ms },
|
ErrorKind::LimitExceeded {
|
||||||
|
retry_after: retry_after.map(|dur| RetryAfter::Delay(dur)),
|
||||||
|
},
|
||||||
"Rate limit exceeded.",
|
"Rate limit exceeded.",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,10 +115,30 @@ pub enum Restriction {
|
||||||
CatchAll,
|
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)]
|
#[derive(Clone, Copy, Debug, Deserialize)]
|
||||||
pub struct Limitation {
|
pub struct Limitation {
|
||||||
#[serde(default = "default_non_zero")]
|
#[serde(default = "default_non_zero", flatten)]
|
||||||
pub per_minute: NonZeroU64,
|
pub timeframe: Timeframe,
|
||||||
#[serde(default = "default_non_zero")]
|
#[serde(default = "default_non_zero")]
|
||||||
pub burst_capacity: NonZeroU64,
|
pub burst_capacity: NonZeroU64,
|
||||||
#[serde(default = "default_non_zero")]
|
#[serde(default = "default_non_zero")]
|
||||||
|
@ -340,7 +360,7 @@ pub fn default_rate_limit() -> BTreeMap<Restriction, Limitation> {
|
||||||
BTreeMap::from_iter([(
|
BTreeMap::from_iter([(
|
||||||
Restriction::default(),
|
Restriction::default(),
|
||||||
Limitation {
|
Limitation {
|
||||||
per_minute: default_non_zero(),
|
timeframe: Timeframe::PerMinute(default_non_zero()),
|
||||||
burst_capacity: default_non_zero(),
|
burst_capacity: default_non_zero(),
|
||||||
weight: default_non_zero(),
|
weight: default_non_zero(),
|
||||||
},
|
},
|
||||||
|
|
|
@ -78,7 +78,7 @@ impl Service {
|
||||||
|
|
||||||
tracing::info!(?limit);
|
tracing::info!(?limit);
|
||||||
|
|
||||||
let increment = 1_000_000_000u64 / limit.per_minute.get() * limit.weight.get();
|
let increment = 1_000_000_000u64 / limit.timeframe.nano_gap() * limit.weight.get();
|
||||||
tracing::info!(?increment);
|
tracing::info!(?increment);
|
||||||
|
|
||||||
let mut prev_expectation = self
|
let mut prev_expectation = self
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue