Add rate limiting to message sending.

This commit is contained in:
Paul
2021-08-06 17:22:55 +01:00
committed by Paul Makles
parent 6fe0725799
commit f09fde616a
6 changed files with 72 additions and 7 deletions

View File

@@ -1,11 +1,22 @@
use rocket_governor::{Method, Quota, RocketGovernable};
use rocket_governor::{Method, Quota, RocketGovernable, RocketGovernor, NonZeroU32};
use phf::phf_map;
pub struct RateLimitGuard;
static ROUTE_QUOTAS: phf::Map<&'static str, &'static Quota> = phf_map! {
"message_send" => &Quota::per_second(NonZeroU32::new(1u32).unwrap())
.allow_burst(NonZeroU32::new(10u32).unwrap())
};
impl<'r> RocketGovernable<'r> for RateLimitGuard {
fn quota(_method: Method, _route_name: &str) -> Quota {
dbg!(_method, _route_name);
Quota::per_second(Self::nonzero(1u32))
.allow_burst(Self::nonzero(10u32))
fn quota(_method: Method, route: &str) -> Quota {
if let Some(q) = ROUTE_QUOTAS.get(route) {
**q
} else {
Quota::per_second(Self::nonzero(1u32))
.allow_burst(Self::nonzero(5u32))
}
}
}
pub type RateLimited<'a> = RocketGovernor<'a, RateLimitGuard>;