Skip to content

Commit caab1f8

Browse files
committed
remove support for custom time frame in rate limiter
1 parent 6cf3556 commit caab1f8

5 files changed

Lines changed: 8 additions & 19 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ limits:
4242

4343
# Configuration for the built-in rate limiter.
4444
rate_limiter:
45-
time_frame: 1 # second(s)
46-
# Maximum amount of requests a client can make within the specified time frame above.
45+
# Maximum amount of requests a client can make per second.
4746
max_requests: 20
48-
# Maximum amount of connections that may be accepted within the specified time frame above.
47+
# Maximum amount of connections that may be accepted within 1 second.
4948
max_connections: 4
5049

5150
# Logging configuration

src/server/client_handle.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
};
77
use crate::{error::Error, server::db::DatabaseBackend};
88
use pwmp_client::pwmp_msg::{request::Request, response::Response};
9-
use std::{io::Read, net::SocketAddr, sync::Arc, time::Duration};
9+
use std::{io::Read, net::SocketAddr, sync::Arc};
1010
use tokio::{net::TcpStream, time::timeout};
1111
use tracing::{debug, error, warn};
1212

@@ -18,10 +18,7 @@ pub async fn handle_client(
1818
config: Arc<Config>,
1919
) -> Result<(), Error> {
2020
let client = Client::new(client, peer_addr);
21-
let mut rate_limiter = RateLimiter::new(
22-
Duration::from_secs(config.rate_limits.time_frame),
23-
config.rate_limits.max_requests,
24-
);
21+
let mut rate_limiter = RateLimiter::new(config.rate_limits.max_requests);
2522
let mut client = client.authorize(db).await?;
2623

2724
loop {

src/server/config.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ pub struct LimitsConfig {
5353

5454
#[derive(Debug, Serialize, Deserialize)]
5555
pub struct RateLimitConfig {
56-
pub time_frame: u64,
5756
pub max_requests: usize,
5857
pub max_connections: usize,
5958
}
@@ -67,7 +66,6 @@ pub struct LogConfig {
6766
impl Default for RateLimitConfig {
6867
fn default() -> Self {
6968
Self {
70-
time_frame: 1,
7169
max_requests: 20,
7270
max_connections: 4,
7371
}

src/server/handle.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{config::Config, db::DatabaseClient, rate_limit::RateLimiter};
22
use crate::server::client_handle::handle_client;
33
use semaphore::Semaphore;
4-
use std::{net::SocketAddr, panic, sync::Arc, time::Duration};
4+
use std::{net::SocketAddr, panic, sync::Arc};
55
use tokio::{
66
net::{TcpListener, TcpStream},
77
runtime::Handle,
@@ -20,10 +20,7 @@ pub async fn server_loop(
2020
) {
2121
let shared_db = Arc::new(db);
2222
let connections = Semaphore::new(config.limits.devices as _, ());
23-
let mut rate_limiter = RateLimiter::new(
24-
Duration::from_secs(config.rate_limits.time_frame),
25-
config.rate_limits.max_connections,
26-
);
23+
let mut rate_limiter = RateLimiter::new(config.rate_limits.max_connections);
2724

2825
loop {
2926
select! {

src/server/rate_limit.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
use std::time::{Duration, Instant};
22

33
pub struct RateLimiter {
4-
time_frame: Duration,
54
time: Instant,
65
max_hits: usize,
76
hits: usize,
87
}
98

109
impl RateLimiter {
11-
pub fn new(time_frame: Duration, max_hits: usize) -> Self {
10+
pub fn new(max_hits: usize) -> Self {
1211
Self {
13-
time_frame,
1412
time: Instant::now(),
1513
max_hits,
1614
hits: 0,
1715
}
1816
}
1917

2018
pub fn hit(&mut self) -> bool {
21-
if self.time.elapsed() >= self.time_frame {
19+
if self.time.elapsed() >= Duration::from_secs(1) {
2220
self.hits = 0;
2321
self.time = Instant::now();
2422
}

0 commit comments

Comments
 (0)