Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · rate

Docs

std/rate

On this page

Index

Constants

INF

const INF: Float = 1.7976931348623157e308

A limit that allows every event (rate.Inf).

Source: std/rate.xo:9

Types

LimitState

struct LimitState {
  limit: Float
  burst: Int
  tokens: Float
  last: Time
  last_event: Time
}

A limiter’s state. Internal: use the methods.

Source: std/rate.xo:12

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.

Source: std/rate.xo:22

Limiter.limit

fn (l: Limiter) limit() -> Float

Source: std/rate.xo:57

Limiter.burst

fn (l: Limiter) burst() -> Int

Source: std/rate.xo:58

Limiter.tokens

fn (l: Limiter) tokens() -> Float uses clock

The tokens in the bucket now (negative while reservations wait).

Source: std/rate.xo:60

Limiter.allow

fn (l: Limiter) allow() -> Bool uses clock

Whether one event may happen now; takes its token if so (rate.Allow).

Source: std/rate.xo:62

Limiter.allow_n

fn (l: Limiter) allow_n(n: Int) -> Bool uses clock

Whether n events may happen now; takes n tokens if so.

Source: std/rate.xo:64

Limiter.reserve

fn (l: Limiter) reserve() -> Reservation uses clock

Reserves 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.

Source: std/rate.xo:67

Limiter.reserve_n

fn (l: Limiter) reserve_n(n: Int) -> Reservation uses clock

Source: std/rate.xo:68

Limiter.wait

fn (l: Limiter) wait() -> () ! RateErr uses clock

Sleeps on the clock until one event may happen (rate.Wait).

Source: std/rate.xo:70

Limiter.wait_n

fn (l: Limiter) wait_n(n: Int) -> () ! RateErr uses clock

Sleeps until n events may happen; Exceeds when n is over the burst.

Source: std/rate.xo:72

Limiter.set_limit

fn (l: Limiter) set_limit(limit: Float) uses clock

Changes the limit, the tokens so far kept (rate.SetLimit).

Source: std/rate.xo:74

Limiter.set_burst

fn (l: Limiter) set_burst(burst: Int) uses clock

Changes the burst (rate.SetBurst).

Source: std/rate.xo:76

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).

Source: std/rate.xo:28

Reservation.ok

fn (r: Reservation) ok() -> Bool

False when the limiter can never allow the event (n over the burst).

Source: std/rate.xo:79

Reservation.delay

fn (r: Reservation) delay() -> Duration uses clock

How long to wait before acting: 0 when now is fine; the longest Duration when not ok.

Source: std/rate.xo:82

Reservation.cancel

fn (r: Reservation) cancel() uses clock

Gives back the reservation’s tokens, as far as later reservations have not counted on them (rate.CancelAt).

Source: std/rate.xo:85

RateErr

enum RateErr {
  Exceeds(n: Int, burst: Int)
  Canceled
}

Why wait failed.

Source: std/rate.xo:41

RateErr.display

fn (e: RateErr) display() -> Str

rate: Wait(n=5) exceeds limiter's burst 3, rate: canceled.

Source: std/rate.xo:49

Functions

new

fn new(limit: Float, burst: Int, clock: Clock) -> Limiter

A limiter of limit tokens a second and a bucket of burst, full (rate.NewLimiter); limit 0 with burst 0 allows nothing, INF everything.

Source: std/rate.xo:53

every

fn every(interval: Duration) -> Float

The limit of one token per interval (rate.Every; INF for 0 or less).

Source: std/rate.xo:55

↑ index