Duration

Enum Duration 

Source
pub enum Duration {
    Seconds(u64),
    Minutes(u64),
    Hours(u64),
    Days(u64),
}

Variants§

§

Seconds(u64)

§

Minutes(u64)

§

Hours(u64)

§

Days(u64)

Implementations§

Source§

impl Duration

Source

pub fn seconds(n: u64) -> Self

Examples found in repository?
examples/demo.rs (line 135)
131async fn main() {
132    println!("Starting lazy-limit demo...\n");
133
134    init_rate_limiter!(
135        default: RuleConfig::new(Duration::seconds(1), 5),
136        max_memory: Some(64 * 1024 * 1024),
137        routes: [
138            ("/api/login", RuleConfig::new(Duration::minutes(1), 3)),
139            ("/api/public", RuleConfig::new(Duration::seconds(1), 10)),
140            ("/api/premium", RuleConfig::new(Duration::seconds(1), 20)),
141        ]
142    )
143    .await;
144
145    println!("Rate limiter initialized with rules:");
146    println!("  - Global: 5 requests/second");
147    println!("  - /api/login: 3 requests/minute");
148    println!("  - /api/public: 10 requests/second");
149    println!("  - /api/premium: 20 requests/second");
150    println!();
151
152    println!("--- Test 1: Basic Global Rate Limiting ---");
153    test_basic_limit().await;
154    println!();
155
156    println!("--- Test 2: Route-Specific Rules (with Global Limit) ---");
157    test_route_specific().await;
158    println!();
159
160    println!("--- Test 3: Override Mode ---");
161    test_override_mode().await;
162    println!();
163
164    println!("--- Test 4: Multiple Users ---");
165    test_multiple_users().await;
166    println!();
167
168    println!("--- Test 5: Long Interval Rules ---");
169    test_long_interval().await;
170    println!();
171
172    println!("All demo tests completed.");
173}
Source

pub fn minutes(n: u64) -> Self

Examples found in repository?
examples/demo.rs (line 138)
131async fn main() {
132    println!("Starting lazy-limit demo...\n");
133
134    init_rate_limiter!(
135        default: RuleConfig::new(Duration::seconds(1), 5),
136        max_memory: Some(64 * 1024 * 1024),
137        routes: [
138            ("/api/login", RuleConfig::new(Duration::minutes(1), 3)),
139            ("/api/public", RuleConfig::new(Duration::seconds(1), 10)),
140            ("/api/premium", RuleConfig::new(Duration::seconds(1), 20)),
141        ]
142    )
143    .await;
144
145    println!("Rate limiter initialized with rules:");
146    println!("  - Global: 5 requests/second");
147    println!("  - /api/login: 3 requests/minute");
148    println!("  - /api/public: 10 requests/second");
149    println!("  - /api/premium: 20 requests/second");
150    println!();
151
152    println!("--- Test 1: Basic Global Rate Limiting ---");
153    test_basic_limit().await;
154    println!();
155
156    println!("--- Test 2: Route-Specific Rules (with Global Limit) ---");
157    test_route_specific().await;
158    println!();
159
160    println!("--- Test 3: Override Mode ---");
161    test_override_mode().await;
162    println!();
163
164    println!("--- Test 4: Multiple Users ---");
165    test_multiple_users().await;
166    println!();
167
168    println!("--- Test 5: Long Interval Rules ---");
169    test_long_interval().await;
170    println!();
171
172    println!("All demo tests completed.");
173}
Source

pub fn hours(n: u64) -> Self

Source

pub fn days(n: u64) -> Self

Source

pub fn as_seconds(&self) -> u64

Source

pub fn is_short_interval(&self) -> bool

Trait Implementations§

Source§

impl Clone for Duration

Source§

fn clone(&self) -> Duration

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Duration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for Duration

Source§

fn cmp(&self, other: &Duration) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Duration

Source§

fn eq(&self, other: &Duration) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Duration

Source§

fn partial_cmp(&self, other: &Duration) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Duration

Source§

impl Eq for Duration

Source§

impl StructuralPartialEq for Duration

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.