token_bucket/gcra/impl.rs
1use super::*;
2
3/// Configuration constructors and accessors for `GcraConfig`.
4impl GcraConfig {
5 /// Builds a `GcraConfig` from an exact emission interval in nanoseconds and burst size.
6 ///
7 /// # Arguments
8 ///
9 /// - `u64` - Emission interval `T` in nanoseconds (`>= 1`).
10 /// - `u64` - Maximum burst capacity `B` (`>= 1`).
11 ///
12 /// # Returns
13 ///
14 /// - `Result<Self, RateLimitError>` - Validated configuration or an error.
15 #[inline(always)]
16 pub fn from_emission_interval_ns(
17 emission_interval_ns: u64,
18 burst: u64,
19 ) -> Result<Self, RateLimitError> {
20 if emission_interval_ns == 0 {
21 return Err(RateLimitError::ZeroPeriod);
22 }
23 if burst == 0 {
24 return Err(RateLimitError::ZeroBurst);
25 }
26 let burst_offset_ns: u128 = (emission_interval_ns as u128)
27 .checked_mul(burst as u128)
28 .ok_or(RateLimitError::Overflow)?;
29 let interval_div: FastDivU64 = FastDivU64::new(emission_interval_ns);
30 Ok(Self {
31 emission_interval_ns,
32 burst,
33 burst_offset_ns,
34 interval_div,
35 })
36 }
37
38 /// Builds a `GcraConfig` from a `Duration` emission interval and burst size.
39 ///
40 /// # Arguments
41 ///
42 /// - `Duration` - Emission interval between tokens (`>= 1ns`).
43 /// - `u64` - Maximum burst capacity (`>= 1`).
44 ///
45 /// # Returns
46 ///
47 /// - `Result<Self, RateLimitError>` - Validated configuration or an error.
48 #[inline(always)]
49 pub fn from_interval(emission_interval: Duration, burst: u64) -> Result<Self, RateLimitError> {
50 let nanos: u128 = emission_interval.as_nanos();
51 if nanos == 0 {
52 return Err(RateLimitError::ZeroPeriod);
53 }
54 if nanos > u64::MAX as u128 {
55 return Err(RateLimitError::Overflow);
56 }
57 Self::from_emission_interval_ns(nanos as u64, burst)
58 }
59
60 /// Builds a `GcraConfig` from a sustained per-second rate and burst capacity.
61 ///
62 /// # Arguments
63 ///
64 /// - `u64` - Sustained requests per second (`>= 1`, up to `1_000_000_000`).
65 /// - `u64` - Maximum burst capacity (`>= 1`).
66 ///
67 /// # Returns
68 ///
69 /// - `Result<Self, RateLimitError>` - Validated configuration or an error.
70 #[inline(always)]
71 pub fn per_second(rate_per_sec: u64, burst: u64) -> Result<Self, RateLimitError> {
72 if rate_per_sec == 0 {
73 return Err(RateLimitError::ZeroRate);
74 }
75 let interval_ns: u128 = NANOS_PER_SEC / (rate_per_sec as u128);
76 if interval_ns == 0 {
77 return Err(RateLimitError::Overflow);
78 }
79 Self::from_emission_interval_ns(interval_ns as u64, burst)
80 }
81
82 /// Builds a `GcraConfig` allowing `limit` units per `period` with the given `burst`.
83 ///
84 /// # Arguments
85 ///
86 /// - `u64` - Quota units replenished over `period` (`>= 1`).
87 /// - `Duration` - Replenishment period (`>= 1ns`).
88 /// - `u64` - Maximum burst capacity (`>= 1`).
89 ///
90 /// # Returns
91 ///
92 /// - `Result<Self, RateLimitError>` - Validated configuration or an error.
93 #[inline(always)]
94 pub fn per_period(limit: u64, period: Duration, burst: u64) -> Result<Self, RateLimitError> {
95 if limit == 0 {
96 return Err(RateLimitError::ZeroRate);
97 }
98 let period_ns: u128 = period.as_nanos();
99 if period_ns == 0 {
100 return Err(RateLimitError::ZeroPeriod);
101 }
102 let interval_ns: u128 = period_ns / (limit as u128);
103 if interval_ns == 0 || interval_ns > u64::MAX as u128 {
104 return Err(RateLimitError::Overflow);
105 }
106 Self::from_emission_interval_ns(interval_ns as u64, burst)
107 }
108
109 /// Returns the emission interval in nanoseconds.
110 ///
111 /// # Arguments
112 ///
113 /// - `&Self` - The GCRA configuration.
114 ///
115 /// # Returns
116 ///
117 /// - `u64` - Emission interval `T` in nanoseconds.
118 #[inline(always)]
119 pub fn get_emission_interval_ns(&self) -> u64 {
120 self.emission_interval_ns
121 }
122
123 /// Returns the configured burst capacity.
124 ///
125 /// # Arguments
126 ///
127 /// - `&Self` - The GCRA configuration.
128 ///
129 /// # Returns
130 ///
131 /// - `u64` - Burst capacity `B`.
132 #[inline(always)]
133 pub fn get_burst(&self) -> u64 {
134 self.burst
135 }
136
137 /// Returns the precomputed burst offset `B * T` in nanoseconds.
138 ///
139 /// # Arguments
140 ///
141 /// - `&Self` - The GCRA configuration.
142 ///
143 /// # Returns
144 ///
145 /// - `u128` - Burst offset in nanoseconds.
146 #[inline(always)]
147 pub(crate) fn get_burst_offset_ns(&self) -> u128 {
148 self.burst_offset_ns
149 }
150
151 /// Returns the precomputed reciprocal divider for `emission_interval_ns`.
152 ///
153 /// # Returns
154 ///
155 /// - `&FastDivU64` - Reciprocal divider reference.
156 #[inline(always)]
157 pub(crate) fn get_interval_div(&self) -> &FastDivU64 {
158 &self.interval_div
159 }
160
161 /// Computes exact `nanos / emission_interval_ns` without hardware division.
162 ///
163 /// # Arguments
164 ///
165 /// - `u64` - Nanosecond numerator.
166 ///
167 /// # Returns
168 ///
169 /// - `u64` - Exact quotient.
170 #[inline(always)]
171 pub(crate) fn div_interval(&self, nanos: u64) -> u64 {
172 self.get_interval_div().div(nanos)
173 }
174}
175
176/// Default empty `GcraCell` initialized at timestamp `0`.
177impl Default for GcraCell {
178 /// Creates an idle `GcraCell` with TAT equal to `0`.
179 #[inline(always)]
180 fn default() -> Self {
181 Self::new()
182 }
183}
184
185/// Lock-free atomic GCRA operations on `GcraCell`.
186impl GcraCell {
187 /// Creates an idle `GcraCell` with zero accumulated arrival debt.
188 #[inline(always)]
189 pub fn new() -> Self {
190 Self {
191 tat_ns: AtomicU64::new(0),
192 }
193 }
194
195 /// Returns a reference to the underlying atomic TAT storage.
196 ///
197 /// # Arguments
198 ///
199 /// - `&Self` - The GCRA cell.
200 ///
201 /// # Returns
202 ///
203 /// - `&AtomicU64` - Reference to the atomic TAT value.
204 #[inline(always)]
205 pub(crate) fn get_tat_ns(&self) -> &AtomicU64 {
206 &self.tat_ns
207 }
208
209 /// Resets the cell back to the idle state (`TAT = 0`).
210 ///
211 /// # Arguments
212 ///
213 /// - `&Self` - The GCRA cell.
214 #[inline(always)]
215 pub fn reset(&self) {
216 self.get_tat_ns().store(0, Ordering::Release);
217 }
218
219 /// Returns whether the cell currently has zero accumulated debt at `now_ns`.
220 ///
221 /// # Arguments
222 ///
223 /// - `&Self` - The GCRA cell.
224 /// - `u64` - Current monotonic timestamp in nanoseconds.
225 ///
226 /// # Returns
227 ///
228 /// - `bool` - `true` when `TAT <= now_ns`.
229 #[inline(always)]
230 pub fn is_idle_at_ns(&self, now_ns: u64) -> bool {
231 Self::is_idle_atomic(self.get_tat_ns(), now_ns)
232 }
233
234 /// Evaluates current quota availability at `now_ns` without mutating state.
235 ///
236 /// # Arguments
237 ///
238 /// - `&Self` - The GCRA cell.
239 /// - `&GcraConfig` - GCRA configuration.
240 /// - `u64` - Current monotonic timestamp in nanoseconds.
241 ///
242 /// # Returns
243 ///
244 /// - `RateLimitDecision` - Snapshot decision for a single-unit probe.
245 #[inline(always)]
246 pub fn peek_at_ns(&self, config: &GcraConfig, now_ns: u64) -> RateLimitDecision {
247 Self::peek_atomic(self.get_tat_ns(), config, now_ns)
248 }
249
250 /// Atomically checks and consumes `cost` units at monotonic timestamp `now_ns`.
251 ///
252 /// # Arguments
253 ///
254 /// - `&Self` - The GCRA cell.
255 /// - `&GcraConfig` - GCRA configuration.
256 /// - `u64` - Current monotonic timestamp in nanoseconds.
257 /// - `u64` - Request cost in quota units.
258 ///
259 /// # Returns
260 ///
261 /// - `RateLimitDecision` - Admission decision and nanosecond timings.
262 #[inline(always)]
263 pub fn check_n_at_ns(&self, config: &GcraConfig, now_ns: u64, cost: u64) -> RateLimitDecision {
264 Self::check_n_atomic(self.get_tat_ns(), config, now_ns, cost)
265 }
266
267 /// Checks whether a raw atomic GCRA TAT value is idle at `now_ns`.
268 ///
269 /// # Arguments
270 ///
271 /// - `&AtomicU64` - Atomic TAT storage.
272 /// - `u64` - Current monotonic timestamp in nanoseconds.
273 ///
274 /// # Returns
275 ///
276 /// - `bool` - `true` when `TAT <= now_ns`.
277 #[inline(always)]
278 pub(crate) fn is_idle_atomic(tat_atomic: &AtomicU64, now_ns: u64) -> bool {
279 tat_atomic.load(Ordering::Relaxed) <= now_ns
280 }
281
282 /// Non-mutating GCRA availability check against a raw atomic TAT storage.
283 ///
284 /// # Arguments
285 ///
286 /// - `&AtomicU64` - Atomic TAT storage.
287 /// - `&GcraConfig` - GCRA configuration.
288 /// - `u64` - Current monotonic timestamp in nanoseconds.
289 ///
290 /// # Returns
291 ///
292 /// - `RateLimitDecision` - Availability snapshot.
293 #[inline(always)]
294 pub(crate) fn peek_atomic(
295 tat_atomic: &AtomicU64,
296 config: &GcraConfig,
297 now_ns: u64,
298 ) -> RateLimitDecision {
299 let burst_offset_u128: u128 = config.get_burst_offset_ns();
300 let burst: u64 = config.get_burst();
301 if burst_offset_u128 <= u64::MAX as u128 {
302 let burst_offset: u64 = burst_offset_u128 as u64;
303 let tat: u64 = tat_atomic.load(Ordering::Relaxed);
304 let used_ns: u64 = tat.saturating_sub(now_ns);
305 let remaining: u64 = config.div_interval(burst_offset.saturating_sub(used_ns));
306 let allowed: bool = remaining >= 1;
307 let retry_after_ns: u64 = if allowed {
308 0
309 } else {
310 let interval: u64 = config.get_emission_interval_ns();
311 used_ns - (burst_offset - interval)
312 };
313 return RateLimitDecision::new(allowed, remaining, used_ns, retry_after_ns, burst);
314 }
315 Self::peek_atomic_u128(tat_atomic, config, now_ns)
316 }
317
318 /// Cold 128-bit fallback for `peek_atomic` when `burst * interval > u64::MAX`.
319 ///
320 /// # Arguments
321 ///
322 /// - `&AtomicU64` - Atomic TAT storage.
323 /// - `&GcraConfig` - GCRA configuration.
324 /// - `u64` - Current monotonic timestamp in nanoseconds.
325 ///
326 /// # Returns
327 ///
328 /// - `RateLimitDecision` - Availability snapshot.
329 #[cold]
330 #[inline(never)]
331 fn peek_atomic_u128(
332 tat_atomic: &AtomicU64,
333 config: &GcraConfig,
334 now_ns: u64,
335 ) -> RateLimitDecision {
336 let tat: u128 = tat_atomic.load(Ordering::Relaxed) as u128;
337 let now: u128 = now_ns as u128;
338 let effective_tat: u128 = if tat > now { tat } else { now };
339 let used_ns: u128 = effective_tat - now;
340 let burst_offset: u128 = config.get_burst_offset_ns();
341 let interval: u128 = config.get_emission_interval_ns() as u128;
342 let remaining: u64 = (burst_offset.saturating_sub(used_ns) / interval) as u64;
343 let allowed: bool = remaining >= 1;
344 let reset_after_ns: u64 = Self::clamp_u128_to_u64(used_ns);
345 let retry_after_ns: u64 = if allowed {
346 0
347 } else {
348 let need_tat: u128 = effective_tat.saturating_add(interval);
349 let horizon: u128 = now.saturating_add(burst_offset);
350 Self::clamp_u128_to_u64(need_tat.saturating_sub(horizon))
351 };
352 RateLimitDecision::new(
353 allowed,
354 remaining,
355 reset_after_ns,
356 retry_after_ns,
357 config.get_burst(),
358 )
359 }
360
361 /// Lock-free GCRA check and consumption against a raw atomic TAT storage.
362 ///
363 /// # Arguments
364 ///
365 /// - `&AtomicU64` - Atomic TAT storage.
366 /// - `&GcraConfig` - GCRA configuration.
367 /// - `u64` - Current monotonic timestamp in nanoseconds.
368 /// - `u64` - Request cost in quota units.
369 ///
370 /// # Returns
371 ///
372 /// - `RateLimitDecision` - Admission decision and nanosecond timings.
373 #[inline(always)]
374 pub(crate) fn check_n_atomic(
375 tat_atomic: &AtomicU64,
376 config: &GcraConfig,
377 now_ns: u64,
378 cost: u64,
379 ) -> RateLimitDecision {
380 let burst_offset_u128: u128 = config.get_burst_offset_ns();
381 if burst_offset_u128 > u64::MAX as u128 {
382 return Self::check_n_atomic_u128(tat_atomic, config, now_ns, cost);
383 }
384 let burst: u64 = config.get_burst();
385 let burst_offset: u64 = burst_offset_u128 as u64;
386 if cost == 0 {
387 let tat: u64 = tat_atomic.load(Ordering::Relaxed);
388 let used_ns: u64 = tat.saturating_sub(now_ns);
389 let remaining: u64 = config.div_interval(burst_offset.saturating_sub(used_ns));
390 return RateLimitDecision::new(true, remaining, used_ns, 0, burst);
391 }
392 if cost > burst {
393 let tat: u64 = tat_atomic.load(Ordering::Relaxed);
394 let used_ns: u64 = tat.saturating_sub(now_ns);
395 let remaining: u64 = config.div_interval(burst_offset.saturating_sub(used_ns));
396 return RateLimitDecision::new(false, remaining, used_ns, u64::MAX, burst);
397 }
398 let increment: u64 = cost.wrapping_mul(config.get_emission_interval_ns());
399 let max_used_before: u64 = burst_offset - increment;
400 let mut current: u64 = tat_atomic.load(Ordering::Relaxed);
401 loop {
402 let used_before: u64 = current.saturating_sub(now_ns);
403 if used_before > max_used_before {
404 let remaining: u64 = config.div_interval(burst_offset.saturating_sub(used_before));
405 let retry_ns: u64 = used_before - max_used_before;
406 return RateLimitDecision::new(false, remaining, used_before, retry_ns, burst);
407 }
408 let new_used: u64 = used_before + increment;
409 let stored_tat: u64 = now_ns.saturating_add(new_used);
410 match tat_atomic.compare_exchange_weak(
411 current,
412 stored_tat,
413 Ordering::Relaxed,
414 Ordering::Relaxed,
415 ) {
416 Ok(_) => {
417 let remaining: u64 = config.div_interval(burst_offset - new_used);
418 return RateLimitDecision::new(true, remaining, new_used, 0, burst);
419 }
420 Err(observed) => {
421 current = observed;
422 }
423 }
424 }
425 }
426
427 /// Cold 128-bit fallback for `check_n_atomic` when `burst * interval > u64::MAX`.
428 ///
429 /// # Arguments
430 ///
431 /// - `&AtomicU64` - Atomic TAT storage.
432 /// - `&GcraConfig` - GCRA configuration.
433 /// - `u64` - Current monotonic timestamp in nanoseconds.
434 /// - `u64` - Request cost in quota units.
435 ///
436 /// # Returns
437 ///
438 /// - `RateLimitDecision` - Admission decision and nanosecond timings.
439 #[cold]
440 #[inline(never)]
441 fn check_n_atomic_u128(
442 tat_atomic: &AtomicU64,
443 config: &GcraConfig,
444 now_ns: u64,
445 cost: u64,
446 ) -> RateLimitDecision {
447 let burst: u64 = config.get_burst();
448 let interval: u128 = config.get_emission_interval_ns() as u128;
449 let burst_offset: u128 = config.get_burst_offset_ns();
450 let now: u128 = now_ns as u128;
451 if cost == 0 {
452 let tat: u128 = tat_atomic.load(Ordering::Relaxed) as u128;
453 let effective_tat: u128 = if tat > now { tat } else { now };
454 let used_ns: u128 = effective_tat - now;
455 let remaining: u64 = (burst_offset.saturating_sub(used_ns) / interval) as u64;
456 return RateLimitDecision::new(
457 true,
458 remaining,
459 Self::clamp_u128_to_u64(used_ns),
460 0,
461 burst,
462 );
463 }
464 if cost > burst {
465 let tat: u128 = tat_atomic.load(Ordering::Relaxed) as u128;
466 let effective_tat: u128 = if tat > now { tat } else { now };
467 let used_ns: u128 = effective_tat - now;
468 let remaining: u64 = (burst_offset.saturating_sub(used_ns) / interval) as u64;
469 return RateLimitDecision::new(
470 false,
471 remaining,
472 Self::clamp_u128_to_u64(used_ns),
473 u64::MAX,
474 burst,
475 );
476 }
477 let increment: u128 = (cost as u128).saturating_mul(interval);
478 let horizon: u128 = now.saturating_add(burst_offset);
479 let mut current: u64 = tat_atomic.load(Ordering::Relaxed);
480 loop {
481 let current_u128: u128 = current as u128;
482 let effective_tat: u128 = if current_u128 > now {
483 current_u128
484 } else {
485 now
486 };
487 let new_tat: u128 = effective_tat.saturating_add(increment);
488 if new_tat > horizon {
489 let used_ns: u128 = effective_tat - now;
490 let remaining: u64 = (burst_offset.saturating_sub(used_ns) / interval) as u64;
491 let retry_ns: u128 = new_tat - horizon;
492 return RateLimitDecision::new(
493 false,
494 remaining,
495 Self::clamp_u128_to_u64(used_ns),
496 Self::clamp_u128_to_u64(retry_ns),
497 burst,
498 );
499 }
500 let stored_tat: u64 = Self::clamp_u128_to_u64(new_tat);
501 match tat_atomic.compare_exchange_weak(
502 current,
503 stored_tat,
504 Ordering::Relaxed,
505 Ordering::Relaxed,
506 ) {
507 Ok(_) => {
508 let used_ns: u128 = new_tat - now;
509 let remaining: u64 = (burst_offset.saturating_sub(used_ns) / interval) as u64;
510 return RateLimitDecision::new(
511 true,
512 remaining,
513 Self::clamp_u128_to_u64(used_ns),
514 0,
515 burst,
516 );
517 }
518 Err(observed) => {
519 current = observed;
520 }
521 }
522 }
523 }
524
525 /// Clamps a `u128` nanosecond duration into `u64` without panicking.
526 ///
527 /// # Arguments
528 ///
529 /// - `u128` - Value to clamp.
530 ///
531 /// # Returns
532 ///
533 /// - `u64` - Saturated value in `0..=u64::MAX`.
534 #[inline(always)]
535 pub(crate) fn clamp_u128_to_u64(value: u128) -> u64 {
536 if value > u64::MAX as u128 {
537 u64::MAX
538 } else {
539 value as u64
540 }
541 }
542}
543
544/// Core lock-free operations for `GcraLimiter`.
545impl GcraLimiter {
546 /// Creates a new `GcraLimiter` from a validated `GcraConfig`.
547 ///
548 /// # Arguments
549 ///
550 /// - `GcraConfig` - Validated GCRA configuration.
551 #[inline(always)]
552 pub fn new(config: GcraConfig) -> Self {
553 Self {
554 config,
555 clock: MonotonicClock::new(),
556 cell: GcraCell::new(),
557 }
558 }
559
560 /// Creates a new `GcraLimiter` anchored at a caller-supplied `MonotonicClock`.
561 ///
562 /// # Arguments
563 ///
564 /// - `GcraConfig` - Validated GCRA configuration.
565 /// - `MonotonicClock` - Monotonic clock origin.
566 #[inline(always)]
567 pub fn with_clock(config: GcraConfig, clock: MonotonicClock) -> Self {
568 Self {
569 config,
570 clock,
571 cell: GcraCell::new(),
572 }
573 }
574
575 /// Creates a `GcraLimiter` directly from sustained requests per second and burst capacity.
576 ///
577 /// # Arguments
578 ///
579 /// - `u64` - Sustained requests per second (`>= 1`).
580 /// - `u64` - Maximum burst capacity (`>= 1`).
581 ///
582 /// # Returns
583 ///
584 /// - `Result<Self, RateLimitError>` - Constructed limiter or configuration error.
585 #[inline(always)]
586 pub fn per_second(rate_per_sec: u64, burst: u64) -> Result<Self, RateLimitError> {
587 let config: GcraConfig = GcraConfig::per_second(rate_per_sec, burst)?;
588 Ok(Self::new(config))
589 }
590
591 /// Returns the GCRA configuration of this limiter.
592 ///
593 /// # Arguments
594 ///
595 /// - `&Self` - The limiter instance.
596 ///
597 /// # Returns
598 ///
599 /// - `&GcraConfig` - Reference to the configuration.
600 #[inline(always)]
601 pub fn get_config(&self) -> &GcraConfig {
602 &self.config
603 }
604
605 /// Returns the embedded monotonic clock.
606 ///
607 /// # Arguments
608 ///
609 /// - `&Self` - The limiter instance.
610 ///
611 /// # Returns
612 ///
613 /// - `&MonotonicClock` - Reference to the clock.
614 #[inline(always)]
615 pub fn get_clock(&self) -> &MonotonicClock {
616 &self.clock
617 }
618
619 /// Returns a reference to the underlying atomic GCRA cell.
620 ///
621 /// # Arguments
622 ///
623 /// - `&Self` - The limiter instance.
624 ///
625 /// # Returns
626 ///
627 /// - `&GcraCell` - Reference to the atomic state cell.
628 #[inline(always)]
629 pub(crate) fn get_cell(&self) -> &GcraCell {
630 &self.cell
631 }
632
633 /// Checks and consumes 1 unit using the embedded monotonic clock.
634 ///
635 /// # Arguments
636 ///
637 /// - `&Self` - The limiter instance.
638 ///
639 /// # Returns
640 ///
641 /// - `RateLimitDecision` - Admission decision and nanosecond timings.
642 #[inline(always)]
643 pub fn check(&self) -> RateLimitDecision {
644 self.check_n_at_ns(self.get_clock().now_ns(), 1)
645 }
646
647 /// Checks and consumes `cost` units using the embedded monotonic clock.
648 ///
649 /// # Arguments
650 ///
651 /// - `&Self` - The limiter instance.
652 /// - `u64` - Number of quota units to consume.
653 ///
654 /// # Returns
655 ///
656 /// - `RateLimitDecision` - Admission decision and nanosecond timings.
657 #[inline(always)]
658 pub fn check_n(&self, cost: u64) -> RateLimitDecision {
659 self.check_n_at_ns(self.get_clock().now_ns(), cost)
660 }
661
662 /// Checks and consumes 1 unit at an explicit monotonic timestamp in nanoseconds.
663 ///
664 /// # Arguments
665 ///
666 /// - `&Self` - The limiter instance.
667 /// - `u64` - Monotonic timestamp in nanoseconds.
668 ///
669 /// # Returns
670 ///
671 /// - `RateLimitDecision` - Admission decision and nanosecond timings.
672 #[inline(always)]
673 pub fn check_at_ns(&self, now_ns: u64) -> RateLimitDecision {
674 self.get_cell().check_n_at_ns(self.get_config(), now_ns, 1)
675 }
676
677 /// Checks and consumes `cost` units at an explicit monotonic timestamp in nanoseconds.
678 ///
679 /// # Arguments
680 ///
681 /// - `&Self` - The limiter instance.
682 /// - `u64` - Monotonic timestamp in nanoseconds.
683 /// - `u64` - Number of quota units to consume.
684 ///
685 /// # Returns
686 ///
687 /// - `RateLimitDecision` - Admission decision and nanosecond timings.
688 #[inline(always)]
689 pub fn check_n_at_ns(&self, now_ns: u64, cost: u64) -> RateLimitDecision {
690 self.get_cell()
691 .check_n_at_ns(self.get_config(), now_ns, cost)
692 }
693
694 /// Inspects current availability without consuming quota.
695 ///
696 /// # Arguments
697 ///
698 /// - `&Self` - The limiter instance.
699 ///
700 /// # Returns
701 ///
702 /// - `RateLimitDecision` - Non-mutating availability snapshot.
703 #[inline(always)]
704 pub fn peek(&self) -> RateLimitDecision {
705 self.peek_at_ns(self.get_clock().now_ns())
706 }
707
708 /// Inspects availability at an explicit monotonic timestamp without consuming quota.
709 ///
710 /// # Arguments
711 ///
712 /// - `&Self` - The limiter instance.
713 /// - `u64` - Monotonic timestamp in nanoseconds.
714 ///
715 /// # Returns
716 ///
717 /// - `RateLimitDecision` - Non-mutating availability snapshot.
718 #[inline(always)]
719 pub fn peek_at_ns(&self, now_ns: u64) -> RateLimitDecision {
720 self.get_cell().peek_at_ns(self.get_config(), now_ns)
721 }
722
723 /// Resets the limiter state back to full burst availability.
724 ///
725 /// # Arguments
726 ///
727 /// - `&Self` - The limiter instance.
728 #[inline(always)]
729 pub fn reset(&self) {
730 self.get_cell().reset();
731 }
732}