Skip to content

Commit

Permalink
Merge pull request #91 from DrVilepis/main
Browse files Browse the repository at this point in the history
rewrite ratelimiter using governor
  • Loading branch information
lajp authored Oct 17, 2023
2 parents 2e40e46 + 39fe30c commit 1c10070
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 219 deletions.
117 changes: 71 additions & 46 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ lto = true

[dependencies]
actix-web = { version = "4.2.1", features = ["macros", "rustls"] }
actix = "0.13"
awc = { version = "3.0.0", features = ["rustls"], optional = true }
actix-cors = "0.6"
http = "0.2"
Expand Down Expand Up @@ -45,3 +44,4 @@ dotenv = "0.15"
url = "2.2"

itertools = "0.10.3"
governor = "0.6.0"
19 changes: 12 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ mod requests;
mod schema;
mod utils;

use actix::Actor;
use std::{num::NonZeroU32, sync::Arc};

use actix_cors::Cors;
use actix_web::{
dev::{ServiceRequest, ServiceResponse},
Expand All @@ -29,7 +30,8 @@ use diesel::{
r2d2::{ConnectionManager, Pool},
PgConnection,
};
use ratelimiter::{RateLimiter, RateLimiterStorage};
use governor::{Quota, RateLimiter};
use ratelimiter::TestaustimeRateLimiter;
use serde_derive::Deserialize;
use tracing::Span;
use tracing_actix_web::{root_span, RootSpanBuilder, TracingLogger};
Expand Down Expand Up @@ -99,7 +101,12 @@ async fn main() -> std::io::Result<()> {
storage: DashMap::new(),
});

let ratelimiter = RateLimiterStorage::new(config.max_requests_per_min, 60).start();
let ratelimiter = Arc::new(
RateLimiter::keyed(Quota::per_minute(
NonZeroU32::new(config.max_requests_per_min as u32).unwrap(),
))
.with_middleware(),
);

let heartbeat_store = Data::new(api::activity::HeartBeatMemoryStore::new());
let leaderboard_cache = Data::new(api::leaderboards::LeaderboardCache::new());
Expand Down Expand Up @@ -134,12 +141,10 @@ async fn main() -> std::io::Result<()> {
let scope = web::scope("")
.wrap(tracing)
.wrap(AuthMiddleware)
.wrap(RateLimiter {
storage: ratelimiter.clone(),
.wrap(TestaustimeRateLimiter {
limiter: Arc::clone(&ratelimiter),
use_peer_addr: config.ratelimit_by_peer_ip,
maxrpm: config.max_requests_per_min,
bypass_token: config.bypass_token.clone(),
reset_interval: 60,
})
.service({
web::scope("/activity")
Expand Down
Loading

0 comments on commit 1c10070

Please sign in to comment.