Skip to content

Commit 19c7908

Browse files
fix(rate-limiter): cargo fmt and pylint E0606 for CI
- Run cargo fmt on engine.rs and benches/rate_limiter.rs - Add _RateLimiterEngine sentinel assignment to fix pylint possibly-used-before-assignment (E0606) when RATE_LIMITER_FORCE_PYTHON skips the import block Signed-off-by: Pratik Gandhi <gandhipratik203@gmail.com>
1 parent f6cf068 commit 19c7908

3 files changed

Lines changed: 59 additions & 14 deletions

File tree

plugins/rate_limiter/rate_limiter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
# ---------------------------------------------------------------------------
5959

6060
_RATE_LIMITER_FORCE_PYTHON = os.environ.get("RATE_LIMITER_FORCE_PYTHON", "").strip().lower() in ("1", "true", "yes")
61+
_RateLimiterEngine: Any = None # Assigned below when the Rust extension is available.
6162

6263
if _RATE_LIMITER_FORCE_PYTHON:
6364
_RUST_AVAILABLE = False

plugins_rust/rate_limiter/benches/rate_limiter.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ use std::hint::black_box;
1212
use std::sync::Arc;
1313

1414
use criterion::{Criterion, criterion_group, criterion_main};
15-
use rate_limiter_rust::{
16-
clock::FakeClock,
17-
config::Algorithm,
18-
memory::MemoryStore,
19-
};
15+
use rate_limiter_rust::{clock::FakeClock, config::Algorithm, memory::MemoryStore};
2016

2117
const T0_UNIX: i64 = 1_000_000;
2218
const LIMIT: u64 = 100;
@@ -90,9 +86,30 @@ fn bench_multi_dim(c: &mut Criterion) {
9086
handle.advance_secs(61);
9187
let now_mono = handle.monotonic_nanos();
9288
let now_unix = handle.unix_secs();
93-
let _r1 = store.check_and_increment("user:alice", LIMIT, WINDOW, Algorithm::FixedWindow, now_mono, now_unix);
94-
let _r2 = store.check_and_increment("tenant:acme", LIMIT * 100, WINDOW, Algorithm::FixedWindow, now_mono, now_unix);
95-
let _r3 = store.check_and_increment("tool:search", LIMIT / 10, WINDOW, Algorithm::FixedWindow, now_mono, now_unix);
89+
let _r1 = store.check_and_increment(
90+
"user:alice",
91+
LIMIT,
92+
WINDOW,
93+
Algorithm::FixedWindow,
94+
now_mono,
95+
now_unix,
96+
);
97+
let _r2 = store.check_and_increment(
98+
"tenant:acme",
99+
LIMIT * 100,
100+
WINDOW,
101+
Algorithm::FixedWindow,
102+
now_mono,
103+
now_unix,
104+
);
105+
let _r3 = store.check_and_increment(
106+
"tool:search",
107+
LIMIT / 10,
108+
WINDOW,
109+
Algorithm::FixedWindow,
110+
now_mono,
111+
now_unix,
112+
);
96113
})
97114
});
98115
}
@@ -133,7 +150,14 @@ fn bench_fixed_window_blocked(c: &mut Criterion) {
133150
let now_mono = handle.monotonic_nanos();
134151
let now_unix = handle.unix_secs();
135152
for _ in 0..LIMIT {
136-
store.check_and_increment("user:blocked", LIMIT, WINDOW, Algorithm::FixedWindow, now_mono, now_unix);
153+
store.check_and_increment(
154+
"user:blocked",
155+
LIMIT,
156+
WINDOW,
157+
Algorithm::FixedWindow,
158+
now_mono,
159+
now_unix,
160+
);
137161
}
138162
// Now every call hits the blocked path
139163
c.bench_function("fixed_window/blocked_path", |b| {

plugins_rust/rate_limiter/src/engine.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use std::sync::Arc;
1818
use pyo3::prelude::*;
1919
use pyo3::types::{PyDict, PyList};
2020

21-
use pyo3_stub_gen::derive::*;
2221
use pyo3_async_runtimes::tokio::future_into_py;
22+
use pyo3_stub_gen::derive::*;
2323

2424
use crate::clock::{Clock, SystemClock};
2525
use crate::config::{ConfigError, EngineConfig};
@@ -312,7 +312,12 @@ impl RateLimiterEngine {
312312
let meta = PyDict::new(py);
313313
meta.set_item("limited", false)?;
314314
let tup = pyo3::types::PyTuple::new(
315-
py, [true.into_pyobject(py)?.to_owned().into_any(), headers.into_any(), meta.into_any()],
315+
py,
316+
[
317+
true.into_pyobject(py)?.to_owned().into_any(),
318+
headers.into_any(),
319+
meta.into_any(),
320+
],
316321
)?;
317322
Ok(tup.into())
318323
})
@@ -334,7 +339,12 @@ impl RateLimiterEngine {
334339
.into_iter()
335340
.map(|(key, limit_count, window_nanos)| {
336341
store.check_and_increment(
337-
&key, limit_count, window_nanos, algorithm, now_mono, now_unix,
342+
&key,
343+
limit_count,
344+
window_nanos,
345+
algorithm,
346+
now_mono,
347+
now_unix,
338348
)
339349
})
340350
.collect()
@@ -350,7 +360,12 @@ impl RateLimiterEngine {
350360
let headers = build_headers_dict(py, &eval, include_retry_after)?;
351361
let meta = build_meta_dict(py, &eval, now_unix)?;
352362
let tup = pyo3::types::PyTuple::new(
353-
py, [eval.allowed.into_pyobject(py)?.to_owned().into_any(), headers.into_any(), meta.into_any()],
363+
py,
364+
[
365+
eval.allowed.into_pyobject(py)?.to_owned().into_any(),
366+
headers.into_any(),
367+
meta.into_any(),
368+
],
354369
)?;
355370
Ok(tup.into())
356371
})
@@ -365,7 +380,12 @@ impl RateLimiterEngine {
365380
impl RateLimiterEngine {
366381
/// Build dimension checks from engine config.
367382
/// Mirrors Python `_build_rust_checks()` but runs in Rust.
368-
fn build_checks(&self, user: &str, tenant: Option<&str>, tool: &str) -> Vec<(String, u64, u64)> {
383+
fn build_checks(
384+
&self,
385+
user: &str,
386+
tenant: Option<&str>,
387+
tool: &str,
388+
) -> Vec<(String, u64, u64)> {
369389
let mut checks = Vec::with_capacity(3);
370390
if let Some(ref rl) = self.config.by_user {
371391
checks.push((format!("user:{}", user), rl.count, rl.window_nanos));

0 commit comments

Comments
 (0)