Skip to content

Commit

Permalink
motis-proxy: Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
jbruechert committed Feb 24, 2024
1 parent 95430a1 commit 15d6f3e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions motis-proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ enum StartType {
OntripStationStart,
/// The user is already on a train and wants to find connections from there.
/// See [Ontrip Train Start](https://motis-project.de/docs/api/endpoint/intermodal.html#ontrip-train-start) in the MOTIS documentation.
OntripTrainStart
OntripTrainStart,
}

#[derive(Deserialize, Serialize, JsonSchema)]
Expand Down Expand Up @@ -582,7 +582,7 @@ fn rocket() -> _ {
.manage(IpRateLimit::new(
NonZeroUsize::new(config.lru_rate_limit_entries)
.expect("lru_rate_limit_entries must not be zero"),
config.routes_per_minute_limit
config.routes_per_minute_limit,
))
.manage(config)
.mount("/", routes)
Expand Down
17 changes: 11 additions & 6 deletions motis-proxy/src/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use lru::LruCache;
use std::{net::IpAddr, num::NonZeroUsize, time::Duration, sync::{Arc, Mutex}};
use std::{
net::IpAddr,
num::NonZeroUsize,
sync::{Arc, Mutex},
time::Duration,
};

use std::time::Instant;

Expand All @@ -14,7 +19,7 @@ struct Inner {
}

pub struct IpRateLimit {
inner: Arc<Mutex<Inner>>
inner: Arc<Mutex<Inner>>,
}

impl IpRateLimit {
Expand All @@ -24,7 +29,7 @@ impl IpRateLimit {
requests: LruCache::new(size),
rate_limit,
last_cleared: Instant::now(),
}))
})),
}
}

Expand All @@ -44,11 +49,11 @@ impl IpRateLimit {
inner.requests.put(*ip, 1);
}


// Check if ip has reached rate limit
let rate_limit = inner.rate_limit;
inner.requests
.get(&ip)
inner
.requests
.get(ip)
.map(|count| *count >= rate_limit)
.unwrap_or(false)
}
Expand Down

0 comments on commit 15d6f3e

Please sign in to comment.