mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 08:25:16 +00:00
Start to implement requests limit
This commit is contained in:
@ -57,6 +57,9 @@ pub mod database_tables_names {
|
||||
pub const NOTIFICATIONS_TABLE: &str = "comunic_notifications";
|
||||
}
|
||||
|
||||
/// Lifetime of limit counter (1 hour)
|
||||
pub const LIMIT_COUNTER_LIFETIME: u64 = 60 * 60;
|
||||
|
||||
/// The account image to show for user who do not have any
|
||||
pub const DEFAULT_ACCOUNT_IMAGE: &str = "avatars/0Reverse.png";
|
||||
|
||||
|
@ -18,6 +18,7 @@ use crate::controllers::routes::{get_routes, RequestResult, Route};
|
||||
use crate::controllers::routes::Method::{GET, POST};
|
||||
use crate::data::config::Config;
|
||||
use crate::data::http_request_handler::{HttpRequestHandler, PostFile, RequestValue};
|
||||
use crate::helpers::requests_limit_helper;
|
||||
|
||||
/// Main server functions
|
||||
///
|
||||
@ -237,6 +238,9 @@ async fn process_request(custom_req: CustomRequest) -> HttpResponse {
|
||||
}
|
||||
let route = route.unwrap();
|
||||
|
||||
// Clean requests limit
|
||||
requests_limit_helper::clean_cache().unwrap();
|
||||
|
||||
// Execute the request
|
||||
let mut request = HttpRequestHandler::new(custom_req.req, custom_req.body);
|
||||
|
||||
@ -280,6 +284,10 @@ async fn process_request(custom_req: CustomRequest) -> HttpResponse {
|
||||
|
||||
/// Given the configuration, start the server
|
||||
pub async fn start_server(conf: &Config) -> std::io::Result<()> {
|
||||
|
||||
// Initialize limit helper
|
||||
requests_limit_helper::init();
|
||||
|
||||
let addr = conf.server_listen_address();
|
||||
println!("Start to listen on http://{}/", addr);
|
||||
|
||||
|
@ -15,4 +15,5 @@ pub mod movies_helper;
|
||||
pub mod survey_helper;
|
||||
pub mod comments_helper;
|
||||
pub mod notifications_helper;
|
||||
pub mod webapp_helper;
|
||||
pub mod webapp_helper;
|
||||
pub mod requests_limit_helper;
|
60
src/helpers/requests_limit_helper.rs
Normal file
60
src/helpers/requests_limit_helper.rs
Normal file
@ -0,0 +1,60 @@
|
||||
//! # Requests limit helper
|
||||
//!
|
||||
//! Handle the limitation of requests, depending on threshold criterias
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::constants::LIMIT_COUNTER_LIFETIME;
|
||||
use crate::data::error::ResultBoxError;
|
||||
use crate::utils::date_utils;
|
||||
|
||||
/// Information about a IP address limitation
|
||||
struct IpInfo {
|
||||
time_start: u64,
|
||||
count: u64,
|
||||
}
|
||||
|
||||
/// Limits cache
|
||||
type Cache = Arc<dashmap::DashMap<String, IpInfo>>;
|
||||
|
||||
static mut LIMITS_CACHE: Option<Arc<Mutex<Cache>>> = None;
|
||||
|
||||
/// Initialize limit cache storage
|
||||
pub fn init() {
|
||||
let limits_cache = Arc::new(dashmap::DashMap::new());
|
||||
let limits_cache = Some(Arc::new(Mutex::new(limits_cache)));
|
||||
|
||||
unsafe {
|
||||
LIMITS_CACHE = limits_cache;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get access to the cache. This resource must absolutely be released as quickly as possible
|
||||
fn get_cache() -> ResultBoxError<Cache> {
|
||||
let cache: Cache;
|
||||
|
||||
unsafe {
|
||||
let guard = LIMITS_CACHE.as_ref().unwrap().lock();
|
||||
cache = guard.unwrap().clone();
|
||||
}
|
||||
|
||||
Ok(cache)
|
||||
}
|
||||
|
||||
/// Clean cached information
|
||||
pub fn clean_cache() -> ResultBoxError {
|
||||
let time = date_utils::time();
|
||||
|
||||
let cache = get_cache()?;
|
||||
let obsolete_ips: Vec<String> = cache
|
||||
.iter()
|
||||
.filter(|k| k.time_start + LIMIT_COUNTER_LIFETIME < time)
|
||||
.map(|k| k.key().to_string())
|
||||
.collect();
|
||||
|
||||
for obsolete_ip in obsolete_ips {
|
||||
cache.remove(&obsolete_ip);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user