update ratelimiter (#897)

* update ratelimiter

* Switch to old scheduler
This commit is contained in:
Geometrically
2024-03-27 15:56:29 -07:00
committed by GitHub
parent a0aa350a08
commit 0a0837ea02
12 changed files with 287 additions and 602 deletions

View File

@@ -1,16 +1,17 @@
use actix_web::{App, HttpServer};
use actix_web_prom::PrometheusMetricsBuilder;
use env_logger::Env;
use governor::middleware::StateInformationMiddleware;
use governor::{Quota, RateLimiter};
use labrinth::database::redis::RedisPool;
use labrinth::file_hosting::S3Host;
use labrinth::ratelimit::errors::ARError;
use labrinth::ratelimit::memory::{MemoryStore, MemoryStoreActor};
use labrinth::ratelimit::middleware::RateLimiter;
use labrinth::search;
use labrinth::util::env::parse_var;
use labrinth::util::ratelimit::{KeyedRateLimiter, RateLimit};
use labrinth::{check_env_vars, clickhouse, database, file_hosting, queue};
use log::{error, info};
use std::num::NonZeroU32;
use std::sync::Arc;
use std::time::Duration;
#[cfg(feature = "jemalloc")]
#[global_allocator]
@@ -90,17 +91,14 @@ async fn main() -> std::io::Result<()> {
let maxmind_reader = Arc::new(queue::maxmind::MaxMindIndexer::new().await.unwrap());
let store = MemoryStore::new();
let prometheus = PrometheusMetricsBuilder::new("labrinth")
.endpoint("/metrics")
.build()
.expect("Failed to create prometheus metrics middleware");
let search_config = search::SearchConfig::new(None);
info!("Starting Actix HTTP server!");
let labrinth_config = labrinth::app_setup(
let mut labrinth_config = labrinth::app_setup(
pool.clone(),
redis_pool.clone(),
search_config.clone(),
@@ -109,32 +107,14 @@ async fn main() -> std::io::Result<()> {
maxmind_reader.clone(),
);
info!("Starting Actix HTTP server!");
// Init App
HttpServer::new(move || {
App::new()
.wrap(prometheus.clone())
.wrap(RateLimit(Arc::clone(&labrinth_config.rate_limiter)))
.wrap(actix_web::middleware::Compress::default())
.wrap(
RateLimiter::new(MemoryStoreActor::from(store.clone()).start())
.with_identifier(|req| {
let connection_info = req.connection_info();
let ip =
String::from(if parse_var("CLOUDFLARE_INTEGRATION").unwrap_or(false) {
if let Some(header) = req.headers().get("CF-Connecting-IP") {
header.to_str().map_err(|_| ARError::Identification)?
} else {
connection_info.peer_addr().ok_or(ARError::Identification)?
}
} else {
connection_info.peer_addr().ok_or(ARError::Identification)?
});
Ok(ip)
})
.with_interval(std::time::Duration::from_secs(60))
.with_max_requests(300)
.with_ignore_key(dotenvy::var("RATE_LIMIT_IGNORE_KEY").ok()),
)
.wrap(sentry_actix::Sentry::new())
.configure(|cfg| labrinth::app_config(cfg, labrinth_config.clone()))
})