std/rate
On this page
Index
- const
INF - type
LimitState - type
Limiter- method
Limiter.limit - method
Limiter.burst - method
Limiter.tokens - method
Limiter.allow - method
Limiter.allow_n - method
Limiter.reserve - method
Limiter.reserve_n - method
Limiter.wait - method
Limiter.wait_n - method
Limiter.set_limit - method
Limiter.set_burst
- method
- type
Reservation- method
Reservation.ok - method
Reservation.delay - method
Reservation.cancel
- method
- type
RateErr- method
RateErr.display
- method
- fn
new - fn
every
Constants
INF
const INF: Float = 1.7976931348623157e308A limit that allows every event (rate.Inf).
Types
LimitState
struct LimitState {
limit: Float
burst: Int
tokens: Float
last: Time
last_event: Time
}A limiter’s state. Internal: use the methods.
Limiter
struct Limiter {
st: Mutex[LimitState]
clock: Clock
}A token bucket: limit tokens a second, at most burst at once.
A handle: copies share the bucket, and tasks may use it at once.
Limiter.limit
fn (l: Limiter) limit() -> FloatLimiter.burst
fn (l: Limiter) burst() -> IntLimiter.tokens
fn (l: Limiter) tokens() -> Float uses clockThe tokens in the bucket now (negative while reservations wait).
Limiter.allow
fn (l: Limiter) allow() -> Bool uses clockWhether one event may happen now; takes its token if so (rate.Allow).
Limiter.allow_n
fn (l: Limiter) allow_n(n: Int) -> Bool uses clockWhether n events may happen now; takes n tokens if so.
Limiter.reserve
fn (l: Limiter) reserve() -> Reservation uses clockReserves a token for an event: act after delay(), or cancel() to
give the token back (rate.Reserve). Not ok when n is over the burst.
Limiter.reserve_n
fn (l: Limiter) reserve_n(n: Int) -> Reservation uses clockLimiter.wait
fn (l: Limiter) wait() -> () ! RateErr uses clockSleeps on the clock until one event may happen (rate.Wait).
Limiter.wait_n
fn (l: Limiter) wait_n(n: Int) -> () ! RateErr uses clockSleeps until n events may happen; Exceeds when n is over the burst.
Limiter.set_limit
fn (l: Limiter) set_limit(limit: Float) uses clockChanges the limit, the tokens so far kept (rate.SetLimit).
Limiter.set_burst
fn (l: Limiter) set_burst(burst: Int) uses clockChanges the burst (rate.SetBurst).
Reservation
struct Reservation {
ok: Bool
lim: Limiter
tokens: Int
time_to_act: Time
limit: Float
made: Time
wait: Duration
}The tokens an event reserved and when it may happen (rate.Reservation).
Reservation.ok
fn (r: Reservation) ok() -> BoolFalse when the limiter can never allow the event (n over the burst).
Reservation.delay
fn (r: Reservation) delay() -> Duration uses clockHow long to wait before acting: 0 when now is fine; the longest Duration when not ok.
Reservation.cancel
fn (r: Reservation) cancel() uses clockGives back the reservation’s tokens, as far as later reservations have not counted on them (rate.CancelAt).
RateErr
enum RateErr {
Exceeds(n: Int, burst: Int)
Canceled
}Why wait failed.
RateErr.display
fn (e: RateErr) display() -> Strrate: Wait(n=5) exceeds limiter's burst 3, rate: canceled.
Functions
new
fn new(limit: Float, burst: Int, clock: Clock) -> LimiterA limiter of limit tokens a second and a bucket of burst, full (rate.NewLimiter); limit 0 with burst 0 allows nothing, INF everything.
every
fn every(interval: Duration) -> FloatThe limit of one token per interval (rate.Every; INF for 0 or less).
↑ index