Adding a search rate limit.

This commit is contained in:
Dessalines 2022-03-25 11:55:24 -04:00
parent 5e47231363
commit 4eb7d81181
5 changed files with 15 additions and 1 deletions

View file

@ -36,6 +36,9 @@
comment: 6
# Interval length for comment limit, in seconds
comment_per_second: 600
search: 6
# Interval length for search limit, in seconds
search_per_second: 600
}
# Settings related to activitypub federation
federation: {

View file

@ -57,6 +57,10 @@ impl RateLimit {
self.kind(RateLimitType::Comment)
}
pub fn search(&self) -> RateLimited {
self.kind(RateLimitType::Search)
}
fn kind(&self, type_: RateLimitType) -> RateLimited {
RateLimited {
rate_limiter: self.rate_limiter.clone(),
@ -79,6 +83,7 @@ impl RateLimited {
RateLimitType::Register => (rate_limit.register, rate_limit.register_per_second),
RateLimitType::Image => (rate_limit.image, rate_limit.image_per_second),
RateLimitType::Comment => (rate_limit.comment, rate_limit.comment_per_second),
RateLimitType::Search => (rate_limit.search, rate_limit.search_per_second),
};
let mut limiter = self.rate_limiter.lock();

View file

@ -16,6 +16,7 @@ pub(crate) enum RateLimitType {
Post,
Image,
Comment,
Search,
}
/// Rate limiting based on rate type and IP addr

View file

@ -170,6 +170,11 @@ pub struct RateLimitConfig {
/// Interval length for comment limit, in seconds
#[default(600)]
pub comment_per_second: i32,
#[default(6)]
pub search: i32,
/// Interval length for search limit, in seconds
#[default(600)]
pub search_per_second: i32,
}
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]

View file

@ -29,7 +29,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
)
.service(
web::resource("/search")
.wrap(rate_limit.comment())
.wrap(rate_limit.search())
.route(web::get().to(route_get::<Search>)),
)
.service(