Skip to content

Commit

Permalink
Merge pull request #20 from JerboaBurrow/throttle_cleaning
Browse files Browse the repository at this point in the history
clear throttle ips periodically
  • Loading branch information
Jerboa-app authored Feb 10, 2024
2 parents 255a23f + 00eb1dd commit 3e7f259
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pulse"
version = "0.0.5"
version = "0.0.6"
authors = ["Jerboa"]

edition="2021"
Expand Down
5 changes: 3 additions & 2 deletions src/server/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ impl ServerHttp

let requests: IpThrottler = IpThrottler::new
(
10.0,
5000
config.get_throttle_config().get_max_requests_per_second(),
config.get_throttle_config().get_timeout_millis(),
config.get_throttle_config().get_clear_period_seconds()
);

let throttle_state = Arc::new(Mutex::new(requests));
Expand Down
5 changes: 3 additions & 2 deletions src/server/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ impl Server

let requests: IpThrottler = IpThrottler::new
(
10.0,
5000
config.get_throttle_config().get_max_requests_per_second(),
config.get_throttle_config().get_timeout_millis(),
config.get_throttle_config().get_clear_period_seconds()
);

let throttle_state = Arc::new(Mutex::new(requests));
Expand Down
34 changes: 33 additions & 1 deletion src/server/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,40 @@ use crate::web::{github::model::GithubStats, discord::request::model::Webhook};

pub const CONFIG_PATH: &str = "config.json";

#[derive(Clone, Serialize, Deserialize)]
pub struct ThrottleConfig
{
max_requests_per_second: f64,
timeout_millis: u128,
clear_period_seconds: u64
}

impl ThrottleConfig
{
pub fn get_max_requests_per_second(&self) -> f64
{
self.max_requests_per_second
}

pub fn get_timeout_millis(&self) -> u128
{
self.timeout_millis
}

pub fn get_clear_period_seconds(&self) -> u64
{
self.clear_period_seconds
}
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Config
{
port: u16,
stats_endpoint: Webhook,
cert_path: String,
key_path: String
key_path: String,
throttle: ThrottleConfig
}

impl Config
Expand All @@ -34,6 +61,11 @@ impl Config
{
self.key_path.clone()
}

pub fn get_throttle_config(&self) -> ThrottleConfig
{
self.throttle.clone()
}

}

Expand Down
39 changes: 28 additions & 11 deletions src/web/throttle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use std::net::{SocketAddr, Ipv4Addr, IpAddr};
use std::time::Instant;
use std::time::{Instant, Duration};
use std::sync::Arc;
use tokio::sync::Mutex;

Expand Down Expand Up @@ -32,17 +32,30 @@ pub struct IpThrottler
requests_from: HashMap<Ipv4Addr, Requests>,
max_requests_per_second: f64,
timeout_millis: u128,
clear_period: Duration,
last_clear: Instant
}

impl IpThrottler
{
pub fn new(max_requests_per_second: f64, timeout_millis: u128) -> IpThrottler
pub fn new(max_requests_per_second: f64, timeout_millis: u128, clear_period_seconds: u64) -> IpThrottler
{
IpThrottler
{
requests_from: HashMap::new(),
max_requests_per_second: max_requests_per_second,
timeout_millis: timeout_millis
timeout_millis: timeout_millis,
clear_period: Duration::from_secs(clear_period_seconds),
last_clear: Instant::now()
}
}

pub fn check_clear(&mut self)
{
if self.last_clear.elapsed() > self.clear_period
{
self.requests_from.clear();
self.last_clear = Instant::now();
}
}

Expand Down Expand Up @@ -105,15 +118,19 @@ pub async fn handle_throttle<B>
) -> Result<Response, StatusCode>
{

if state.lock().await.is_limited(addr)
{
Err(StatusCode::TOO_MANY_REQUESTS)
}
else
{
println!("passing on");
let response = next.run(request).await;
Ok(response)
let mut throttler = state.lock().await;
throttler.check_clear();
if throttler.is_limited(addr)
{
Err(StatusCode::TOO_MANY_REQUESTS)
}
else
{
crate::debug(format!("Allowing: {}", addr), None);
let response = next.run(request).await;
Ok(response)
}
}

}

0 comments on commit 3e7f259

Please sign in to comment.