diff --git a/Cargo.lock b/Cargo.lock index a06156be24..8e96f6253a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -434,7 +434,7 @@ dependencies = [ "bitflags 2.4.1", "cexpr", "clang-sys", - "itertools", + "itertools 0.10.5", "log", "prettyplease", "proc-macro2", @@ -843,7 +843,7 @@ dependencies = [ "clap", "criterion-plot", "is-terminal", - "itertools", + "itertools 0.10.5", "num-traits", "once_cell", "oorandom", @@ -864,7 +864,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" dependencies = [ "cast", - "itertools", + "itertools 0.10.5", ] [[package]] @@ -2114,6 +2114,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.5" @@ -3132,7 +3141,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" dependencies = [ "anyhow", - "itertools", + "itertools 0.10.5", "proc-macro2", "quote", "syn 1.0.109", @@ -3551,7 +3560,7 @@ dependencies = [ "chrono", "dynfmt", "insta", - "itertools", + "itertools 0.13.0", "maxminddb", "md5", "once_cell", @@ -3675,7 +3684,7 @@ dependencies = [ "hash32", "hashbrown 0.14.5", "insta", - "itertools", + "itertools 0.13.0", "priority-queue", "rand", "relay-base-schema", @@ -3723,7 +3732,7 @@ version = "24.8.0" dependencies = [ "hmac", "insta", - "itertools", + "itertools 0.13.0", "minidump", "num-traits", "once_cell", @@ -3752,7 +3761,7 @@ dependencies = [ "chrono", "data-encoding", "insta", - "itertools", + "itertools 0.13.0", "relay-base-schema", "relay-event-schema", "relay-log", @@ -3796,7 +3805,7 @@ version = "24.8.0" dependencies = [ "hashbrown 0.14.5", "insta", - "itertools", + "itertools 0.13.0", "relay-base-schema", "relay-common", "relay-log", @@ -3876,7 +3885,7 @@ dependencies = [ "hashbrown 0.14.5", "hyper-util", "insta", - "itertools", + "itertools 0.13.0", "json-forensics", "mime", "minidump", @@ -4766,7 +4775,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c12bc9199d1db8234678b7051747c07f517cdcf019262d1847b94ec8b1aee3e" dependencies = [ - "itertools", + "itertools 0.10.5", "nom", "unicode_categories", ] diff --git a/Cargo.toml b/Cargo.toml index cfc3087e92..17b1486f33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,7 +101,7 @@ hyper-util = { version = "0.1.7", features = ["tokio"] } indexmap = "2.2.5" insta = { version = "1.31.0", features = ["json", "redactions", "ron"] } ipnetwork = "0.20.0" -itertools = "0.10.5" +itertools = "0.13.0" json-forensics = "0.1.1" lru = "0.9.0" maxminddb = "0.23.0" diff --git a/relay-quotas/src/global.rs b/relay-quotas/src/global.rs index 38c81cdbaf..e5a3ea0090 100644 --- a/relay-quotas/src/global.rs +++ b/relay-quotas/src/global.rs @@ -1,11 +1,12 @@ use std::sync::{Mutex, OnceLock, PoisonError}; -use crate::RedisQuota; use itertools::Itertools; use relay_base_schema::metrics::MetricNamespace; use relay_redis::redis::Script; use relay_redis::{PooledClient, RedisError}; +use crate::RedisQuota; + /// Default percentage of the quota limit to reserve from Redis as a local cache. const DEFAULT_BUDGET_RATIO: f32 = 0.001; @@ -37,11 +38,12 @@ impl GlobalRateLimits { let mut ratelimited = vec![]; let mut not_ratelimited = vec![]; - for (key, quotas) in "as.iter().group_by(|quota| KeyRef::new(quota)) { - let Some(quota) = quotas.min_by_key(|quota| quota.limit()) else { - continue; - }; + let min_by_keyref = quotas + .iter() + .into_grouping_map_by(|q| KeyRef::new(q)) + .min_by_key(|_, q| q.limit()); + for (key, quota) in min_by_keyref { let val = guard.entry_ref(&key).or_default(); if val.is_rate_limited(client, quota, key, quantity as u64)? { @@ -243,6 +245,7 @@ impl Default for GlobalRateLimit { #[cfg(test)] mod tests { + use std::collections::BTreeSet; use std::time::Duration; use super::*; @@ -312,13 +315,12 @@ mod tests { .unwrap(); // Only the quotas that are less than the quantity gets ratelimited. - assert_eq!(rate_limited_quotas.len(), 2); assert_eq!( - vec![100, 150], + BTreeSet::from([100, 150]), rate_limited_quotas .iter() .map(|quota| quota.limit()) - .collect_vec(), + .collect() ); }