Concepts

Rate Limits

Requests per second, stored as f64.

Rate limits are expressed as requests per second using RateLimit (a validated wrapper around f64).

RateLimit::try_from(value) fails if value <= 0.

use trypema::RateLimit;

let r = RateLimit::try_from(10.0).unwrap();
let r = RateLimit::try_from(5.5).unwrap();

Converting from "per minute" and friends

Trypema is requests/second, so convert by dividing:

For example, 300/min is 5.0/sec and 1200/min is 20.0/sec.

use trypema::RateLimit;

let per_minute = 300.0;
let per_second = per_minute / 60.0;
let rate = RateLimit::try_from(per_second).unwrap();
let _ = rate;

Window capacity is computed as:

window_capacity = window_size_seconds * rate_limit

Example: 60 second window and 5.0 req/s => capacity 300 requests in the window.

Internally, counters are integers. For non-integer rates, the effective capacity is enforced using integer comparisons, so you should treat the limit as approximate at the edges.

"Unlimited" (for testing)

If you want to track behavior without rejecting (e.g. in tests), you can use RateLimit::max():

RateLimit::max() returns RateLimit(f64::MAX).

use trypema::RateLimit;

let unlimited = RateLimit::max();
let _ = unlimited;