#[non_exhaustive]pub enum Decision {
Allow,
Deny {
retry_after: Duration,
},
}Expand description
The result of checking a key against its limit.
Returned by RateLimiter::check and
check_n. A check is infallible — there is no
error case on the request path, only an allow/deny outcome — so this is a
plain enum rather than a Result. When a request is denied, the decision
carries how long the caller should wait before enough capacity will have
accrued, which is exactly what an HTTP Retry-After header needs.
#[non_exhaustive] so future variants can be added without breaking callers;
match with a wildcard arm, or use the is_allow /
retry_after helpers.
§Examples
use rate_net::{RateLimiter, Decision};
let limiter = RateLimiter::per_second(1);
match limiter.check("user:42") {
Decision::Allow => { /* serve the request */ }
Decision::Deny { retry_after } => {
// return 429 with `Retry-After: {retry_after}`
let _ = retry_after;
}
_ => {}
}Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Allow
The request is within the limit and has been counted against the key.
Deny
The request would exceed the key’s limit and was refused.
Fields
retry_after: DurationThe minimum wait until the same request would be admitted. A value of
Duration::MAX means it can never succeed — the request asked for
more than the limit’s burst capacity.
Implementations§
Source§impl Decision
impl Decision
Sourcepub const fn is_allow(&self) -> bool
pub const fn is_allow(&self) -> bool
Returns true if the request was admitted.
§Examples
use rate_net::Decision;
assert!(Decision::Allow.is_allow());Sourcepub const fn is_deny(&self) -> bool
pub const fn is_deny(&self) -> bool
Returns true if the request was refused.
§Examples
use rate_net::Decision;
use std::time::Duration;
let denied = Decision::Deny { retry_after: Duration::from_millis(250) };
assert!(denied.is_deny());Sourcepub const fn retry_after(&self) -> Option<Duration>
pub const fn retry_after(&self) -> Option<Duration>
Returns the wait until the request would be admitted, or None if it was
allowed.
Use it to populate an HTTP Retry-After header on a 429 response, or
to drive a client-side backoff.
§Examples
use rate_net::Decision;
use std::time::Duration;
let denied = Decision::Deny { retry_after: Duration::from_millis(250) };
assert_eq!(denied.retry_after(), Some(Duration::from_millis(250)));
assert_eq!(Decision::Allow.retry_after(), None);Trait Implementations§
Source§impl From<Decision> for Decision
impl From<Decision> for Decision
Source§fn from(decision: Decision) -> Self
fn from(decision: Decision) -> Self
Lifts a better_bucket::Decision into the rate-net decision. The token
bucket’s Allowed/Denied { retry_after } maps directly; any future
variant added upstream is treated, conservatively, as a denial that can
never succeed.