Skip to content

Commit 4131a20

Browse files
authored
build: Update itertools to 0.13.0 (#3993)
Updates itertools to the latest version. `group_by` has been replaced by `chunk_by` to clarify that this method only collects _consecutive_ elements with the same key. To improve on this, this PR uses `GroupingMap`, which offers more efficient folding operations such as `min_by_key` directly. This implements the desired outcome of collecing the shortest quota for each unique `KeyRef`.
1 parent 61d5fb9 commit 4131a20

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ hyper-util = { version = "0.1.7", features = ["tokio"] }
101101
indexmap = "2.2.5"
102102
insta = { version = "1.31.0", features = ["json", "redactions", "ron"] }
103103
ipnetwork = "0.20.0"
104-
itertools = "0.10.5"
104+
itertools = "0.13.0"
105105
json-forensics = "0.1.1"
106106
lru = "0.9.0"
107107
maxminddb = "0.23.0"

relay-quotas/src/global.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::sync::{Mutex, OnceLock, PoisonError};
22

3-
use crate::RedisQuota;
43
use itertools::Itertools;
54
use relay_base_schema::metrics::MetricNamespace;
65
use relay_redis::redis::Script;
76
use relay_redis::{PooledClient, RedisError};
87

8+
use crate::RedisQuota;
9+
910
/// Default percentage of the quota limit to reserve from Redis as a local cache.
1011
const DEFAULT_BUDGET_RATIO: f32 = 0.001;
1112

@@ -37,11 +38,12 @@ impl GlobalRateLimits {
3738
let mut ratelimited = vec![];
3839
let mut not_ratelimited = vec![];
3940

40-
for (key, quotas) in &quotas.iter().group_by(|quota| KeyRef::new(quota)) {
41-
let Some(quota) = quotas.min_by_key(|quota| quota.limit()) else {
42-
continue;
43-
};
41+
let min_by_keyref = quotas
42+
.iter()
43+
.into_grouping_map_by(|q| KeyRef::new(q))
44+
.min_by_key(|_, q| q.limit());
4445

46+
for (key, quota) in min_by_keyref {
4547
let val = guard.entry_ref(&key).or_default();
4648

4749
if val.is_rate_limited(client, quota, key, quantity as u64)? {
@@ -243,6 +245,7 @@ impl Default for GlobalRateLimit {
243245

244246
#[cfg(test)]
245247
mod tests {
248+
use std::collections::BTreeSet;
246249
use std::time::Duration;
247250

248251
use super::*;
@@ -312,13 +315,12 @@ mod tests {
312315
.unwrap();
313316

314317
// Only the quotas that are less than the quantity gets ratelimited.
315-
assert_eq!(rate_limited_quotas.len(), 2);
316318
assert_eq!(
317-
vec![100, 150],
319+
BTreeSet::from([100, 150]),
318320
rate_limited_quotas
319321
.iter()
320322
.map(|quota| quota.limit())
321-
.collect_vec(),
323+
.collect()
322324
);
323325
}
324326

0 commit comments

Comments
 (0)