Skip to content

Commit 1acf126

Browse files
committed
fix tlru race condition
The previous implementation would crash when accessing entries that were about to be removed.
1 parent e757def commit 1acf126

File tree

5 files changed

+96
-215
lines changed

5 files changed

+96
-215
lines changed

Cargo.lock

Lines changed: 3 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ name = "twilight-http-proxy"
55
version = "0.1.0"
66

77
[dependencies]
8-
dashmap = "6"
98
http = "1"
109
http-body-util = "0.1"
1110
hyper = { version = "1", default-features = false }

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::{
2323
env,
2424
error::Error,
2525
net::{Ipv4Addr, SocketAddrV4},
26+
num::NonZero,
2627
pin::pin,
2728
str::FromStr,
2829
sync::Arc,
@@ -88,7 +89,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
8889
let ratelimiter_map = Arc::new(RatelimiterMap::new(
8990
env::var("DISCORD_TOKEN")?,
9091
Duration::from_secs(parse_env("CLIENT_DECAY_TIMEOUT")?.unwrap_or(3600)),
91-
parse_env("CLIENT_CACHE_MAX_SIZE")?,
92+
parse_env("CLIENT_CACHE_MAX_SIZE")?.unwrap_or(NonZero::<usize>::MAX),
9293
));
9394

9495
let host = parse_env("HOST")?.unwrap_or(Ipv4Addr::UNSPECIFIED);

src/ratelimiter_map.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::tlru::{Builder, Tlru};
1+
use crate::tlru::Tlru;
2+
use std::num::NonZero;
23
use tokio::time::Duration;
34
use twilight_http_ratelimiting::RateLimiter;
45

@@ -9,7 +10,7 @@ pub struct RatelimiterMap {
910
}
1011

1112
impl RatelimiterMap {
12-
pub fn new(mut default_token: String, timeout: Duration, max_size: Option<usize>) -> Self {
13+
pub fn new(mut default_token: String, timeout: Duration, cap: NonZero<usize>) -> Self {
1314
let is_bot = default_token.starts_with("Bot ");
1415
let is_bearer = default_token.starts_with("Bearer ");
1516

@@ -19,13 +20,7 @@ impl RatelimiterMap {
1920
default_token.insert_str(0, "Bot ");
2021
}
2122

22-
let mut builder = Builder::new().expiration(timeout);
23-
24-
if let Some(max_size) = max_size {
25-
builder = builder.max_size(max_size);
26-
}
27-
28-
let inner = builder.build();
23+
let inner = Tlru::new(cap, timeout);
2924

3025
let default = RateLimiter::default();
3126

@@ -41,13 +36,13 @@ impl RatelimiterMap {
4136
if token == self.default_token {
4237
(self.default.clone(), self.default_token.clone())
4338
} else if let Some(entry) = self.inner.get(token) {
44-
(entry.value().clone(), token.to_string())
39+
(entry, token.to_owned())
4540
} else {
4641
let ratelimiter = RateLimiter::default();
4742

48-
self.inner.insert(token.to_string(), ratelimiter.clone());
43+
self.inner.insert(token.to_owned(), ratelimiter.clone());
4944

50-
(ratelimiter, token.to_string())
45+
(ratelimiter, token.to_owned())
5146
}
5247
} else {
5348
(self.default.clone(), self.default_token.clone())

0 commit comments

Comments
 (0)