-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
motis-proxy: Implement route rate limit
- Loading branch information
1 parent
00a5b81
commit baf8fbf
Showing
7 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-FileCopyrightText: 2024 Jonah Brüchert <[email protected]> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
use lru::LruCache; | ||
use std::{net::IpAddr, num::NonZeroUsize, time::Duration, sync::{Arc, Mutex}}; | ||
|
||
use std::time::Instant; | ||
|
||
struct Inner { | ||
requests: LruCache<IpAddr, u16>, | ||
rate_limit: u16, | ||
last_cleared: Instant, | ||
} | ||
|
||
pub struct IpRateLimit { | ||
inner: Arc<Mutex<Inner>> | ||
} | ||
|
||
impl IpRateLimit { | ||
pub fn new(size: NonZeroUsize, rate_limit: u16) -> IpRateLimit { | ||
IpRateLimit { | ||
inner: Arc::new(Mutex::new(Inner { | ||
requests: LruCache::new(size), | ||
rate_limit, | ||
last_cleared: Instant::now(), | ||
})) | ||
} | ||
} | ||
|
||
pub fn should_limit(&self, ip: &IpAddr) -> bool { | ||
let mut inner = self.inner.lock().unwrap(); | ||
|
||
// Check if data is older than a minute, then delete it | ||
if inner.last_cleared.elapsed() >= Duration::from_secs(60) { | ||
inner.requests.clear(); | ||
inner.last_cleared = Instant::now(); | ||
} | ||
|
||
// Incerement existing request counter for address or create a new one | ||
if let Some(count) = inner.requests.get_mut(ip) { | ||
*count += 1; | ||
} else { | ||
inner.requests.put(*ip, 1); | ||
} | ||
|
||
|
||
// Check if ip has reached rate limit | ||
let rate_limit = inner.rate_limit; | ||
inner.requests | ||
.get(&ip) | ||
.map(|count| *count >= rate_limit) | ||
.unwrap_or(false) | ||
} | ||
} |