1 use crate::{settings::structs::RateLimitConfig, utils::get_ip, IpAddr, LemmyError};
2 use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
3 use futures::future::{ok, Ready};
4 use rate_limiter::{RateLimitType, RateLimiter};
11 use tokio::sync::Mutex;
15 #[derive(Debug, Clone)]
16 pub struct RateLimit {
17 // it might be reasonable to use a std::sync::Mutex here, since we don't need to lock this
18 // across await points
19 pub rate_limiter: Arc<Mutex<RateLimiter>>,
20 pub rate_limit_config: RateLimitConfig,
23 #[derive(Debug, Clone)]
24 pub struct RateLimited {
25 rate_limiter: Arc<Mutex<RateLimiter>>,
26 rate_limit_config: RateLimitConfig,
30 pub struct RateLimitedMiddleware<S> {
31 rate_limited: RateLimited,
36 pub fn message(&self) -> RateLimited {
37 self.kind(RateLimitType::Message)
40 pub fn post(&self) -> RateLimited {
41 self.kind(RateLimitType::Post)
44 pub fn register(&self) -> RateLimited {
45 self.kind(RateLimitType::Register)
48 pub fn image(&self) -> RateLimited {
49 self.kind(RateLimitType::Image)
52 fn kind(&self, type_: RateLimitType) -> RateLimited {
54 rate_limiter: self.rate_limiter.clone(),
55 rate_limit_config: self.rate_limit_config.clone(),
62 pub async fn wrap<T, E>(
65 fut: impl Future<Output = Result<T, E>>,
70 // Does not need to be blocking because the RwLock in settings never held across await points,
71 // and the operation here locks only long enough to clone
72 let rate_limit = self.rate_limit_config;
76 let mut limiter = self.rate_limiter.lock().await;
79 RateLimitType::Message => {
80 limiter.check_rate_limit_full(
84 rate_limit.message_per_second,
91 RateLimitType::Post => {
92 limiter.check_rate_limit_full(
96 rate_limit.post_per_second,
100 RateLimitType::Register => {
101 limiter.check_rate_limit_full(
105 rate_limit.register_per_second,
109 RateLimitType::Image => {
110 limiter.check_rate_limit_full(
114 rate_limit.image_per_second,
125 let mut limiter = self.rate_limiter.lock().await;
128 RateLimitType::Post => {
129 limiter.check_rate_limit_full(
133 rate_limit.post_per_second,
137 RateLimitType::Register => {
138 limiter.check_rate_limit_full(
142 rate_limit.register_per_second,
155 impl<S> Transform<S, ServiceRequest> for RateLimited
157 S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
160 type Response = S::Response;
161 type Error = actix_web::Error;
163 type Transform = RateLimitedMiddleware<S>;
164 type Future = Ready<Result<Self::Transform, Self::InitError>>;
166 fn new_transform(&self, service: S) -> Self::Future {
167 ok(RateLimitedMiddleware {
168 rate_limited: self.clone(),
174 type FutResult<T, E> = dyn Future<Output = Result<T, E>>;
176 impl<S> Service<ServiceRequest> for RateLimitedMiddleware<S>
178 S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
181 type Response = S::Response;
182 type Error = actix_web::Error;
183 type Future = Pin<Box<FutResult<Self::Response, Self::Error>>>;
185 fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
186 self.service.poll_ready(cx)
189 fn call(&self, req: ServiceRequest) -> Self::Future {
190 let ip_addr = get_ip(&req.connection_info());
195 .wrap(ip_addr, self.service.call(req));
197 Box::pin(async move { fut.await.map_err(actix_web::Error::from) })