Skip to content

Commit b1dff76

Browse files
committed
Nits
1 parent 023bf2e commit b1dff76

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/rate_limiter.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ use sieve_cache::SieveCache;
77

88
const DEFAULT_CAPACITY: usize = 10000;
99
const DEFAULT_MAX_QPS: u32 = 100;
10-
const MICROTOKENS_PER_TOKEN: u64 = 1_000_000;
10+
const MILLITOKENS_PER_TOKEN: u64 = 1000;
1111

1212
struct ClientState {
13-
microtokens: u64,
13+
millitokens: u64,
1414
last_update: Instant,
1515
}
1616

1717
pub struct RateLimiter {
1818
clients: Mutex<SieveCache<IpAddr, ClientState>>,
19-
max_microtokens: u64,
20-
refill_rate: u64, // microtokens per microsecond (equals max_qps)
19+
max_millitokens: u64,
20+
refill_rate: u64, // millitokens per millisecond
2121
}
2222

2323
impl RateLimiter {
@@ -32,11 +32,12 @@ impl RateLimiter {
3232
} else {
3333
max_queries_per_second
3434
};
35+
// max_qps tokens/second = max_qps millitokens/millisecond
3536
RateLimiter {
3637
clients: Mutex::new(
3738
SieveCache::new(capacity).expect("Failed to create rate limiter cache"),
3839
),
39-
max_microtokens: (max_qps as u64).saturating_mul(MICROTOKENS_PER_TOKEN),
40+
max_millitokens: (max_qps as u64).saturating_mul(MILLITOKENS_PER_TOKEN),
4041
refill_rate: max_qps as u64,
4142
}
4243
}
@@ -46,20 +47,21 @@ impl RateLimiter {
4647
let mut clients = self.clients.lock();
4748

4849
if let Some(state) = clients.get_mut(&client_ip) {
49-
let elapsed_us = now.as_ticks().saturating_sub(state.last_update.as_ticks());
50-
let refill = elapsed_us.saturating_mul(self.refill_rate);
51-
state.microtokens = state.microtokens.saturating_add(refill).min(self.max_microtokens);
50+
let elapsed = now.duration_since(state.last_update);
51+
let elapsed_ms = elapsed.as_millis();
52+
let refill = elapsed_ms.saturating_mul(self.refill_rate);
53+
state.millitokens = state.millitokens.saturating_add(refill).min(self.max_millitokens);
5254
state.last_update = now;
5355

54-
if state.microtokens >= MICROTOKENS_PER_TOKEN {
55-
state.microtokens -= MICROTOKENS_PER_TOKEN;
56+
if state.millitokens >= MILLITOKENS_PER_TOKEN {
57+
state.millitokens -= MILLITOKENS_PER_TOKEN;
5658
true
5759
} else {
5860
false
5961
}
6062
} else {
6163
let state = ClientState {
62-
microtokens: self.max_microtokens.saturating_sub(MICROTOKENS_PER_TOKEN),
64+
millitokens: self.max_millitokens.saturating_sub(MILLITOKENS_PER_TOKEN),
6365
last_update: now,
6466
};
6567
clients.insert(client_ip, state);

0 commit comments

Comments
 (0)