2 use crate::{get_ip, LemmyError};
3 use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
4 use futures::future::{ok, Ready};
5 use lemmy_utils::settings::{RateLimitConfig, Settings};
6 use rate_limiter::{RateLimitType, RateLimiter};
11 task::{Context, Poll},
13 use tokio::sync::Mutex;
17 #[derive(Debug, Clone)]
18 pub struct RateLimit {
19 // it might be reasonable to use a std::sync::Mutex here, since we don't need to lock this
20 // across await points
21 pub rate_limiter: Arc<Mutex<RateLimiter>>,
24 #[derive(Debug, Clone)]
25 pub struct RateLimited {
26 rate_limiter: Arc<Mutex<RateLimiter>>,
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(),
61 pub async fn wrap<T, E>(
64 fut: impl Future<Output = Result<T, E>>,
69 // Does not need to be blocking because the RwLock in settings never held across await points,
70 // and the operation here locks only long enough to clone
71 let rate_limit: RateLimitConfig = Settings::get().rate_limit;
75 let mut limiter = self.rate_limiter.lock().await;
78 RateLimitType::Message => {
79 limiter.check_rate_limit_full(
83 rate_limit.message_per_second,
90 RateLimitType::Post => {
91 limiter.check_rate_limit_full(
95 rate_limit.post_per_second,
99 RateLimitType::Register => {
100 limiter.check_rate_limit_full(
104 rate_limit.register_per_second,
108 RateLimitType::Image => {
109 limiter.check_rate_limit_full(
113 rate_limit.image_per_second,
124 let mut limiter = self.rate_limiter.lock().await;
127 RateLimitType::Post => {
128 limiter.check_rate_limit_full(
132 rate_limit.post_per_second,
136 RateLimitType::Register => {
137 limiter.check_rate_limit_full(
141 rate_limit.register_per_second,
154 impl<S> Transform<S> for RateLimited
156 S: Service<Request = ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
159 type Request = S::Request;
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 for RateLimitedMiddleware<S>
178 S: Service<Request = ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
181 type Request = S::Request;
182 type Response = S::Response;
183 type Error = actix_web::Error;
184 type Future = Pin<Box<FutResult<Self::Response, Self::Error>>>;
186 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
187 self.service.poll_ready(cx)
190 fn call(&mut self, req: S::Request) -> Self::Future {
191 let ip_addr = get_ip(&req.connection_info());
196 .wrap(ip_addr, self.service.call(req));
198 Box::pin(async move { fut.await.map_err(actix_web::Error::from) })