Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear throttle ips periodically #20

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}

}