A visit's event budget (spec/01 §5.1): a token bucket that bounds how fast one visitor's events are acted on, so a host taking events from anonymous visitors needs no wrapper around any of them.
- Set by the host, as
mount/3's andmount_page/3'sevent_budget::[per_second: n, burst: n], each a positive integer and each defaulting (20and40), or:unlimited. The page takes it from its first mount, as it takes its clock. - Spent once per event the runtime acts on: each of the page's client events,
"mw-event","mw-request","mw-size"and"mw-shortcut", before it is read. The bucket holdsburstevents and refills atper_second, read on the page's clock (Macaw.Data.Clock.monotonic_ms/1), so a test steps it with the manual clock. - Past the budget the event is dropped, never acted on and never a crash: the page
counts it and keeps its fault,
{[:event], {:over_budget, %{per_second: n, burst: n}}}, forMacaw.Live.info/1, and sets@macaw.limitedso its template can say so. The next event the budget admits sets it back.
The bucket is a pure value in integer milli-events: spend/2 refills it by the
milliseconds since the last spend and takes one event from it, in constant work.
Summary
Types
The host's option: the rate and burst, either defaulting, or :unlimited.
A bucket: its rate and burst, the milli-events it holds, the clock's ms at its last
spend (nil before its first), the events it refused, whether its last spend was
refused, and the fault of the last one it refused.
Functions
The default option: 20 events a second, a burst of 40.
The faults of an event_budget: option, [] for a good one (spec/01 §2):
{[:event_budget], {:type, :event_budget}} for what is neither a keyword list nor
:unlimited, {[:event_budget, key], :unknown_key}, and {[:event_budget, key], {:type, :pos_integer}} for a rate or burst that isn't a positive integer.
A full bucket for a judged option: nil for :unlimited, which spends nothing.
One event spent at the clock's ms: {:ok, bucket}, refilled by the milliseconds since
the last spend at per_second and one event taken, or {:error, fault, bucket} with the
event refused, counted and its fault kept. A clock read earlier than the last spend
refills nothing.
Types
@type option() :: [per_second: pos_integer(), burst: pos_integer()] | :unlimited
The host's option: the rate and burst, either defaulting, or :unlimited.
@type t() :: %Macaw.Live.Visit.Budget{ at: integer() | nil, burst: pos_integer(), dropped: non_neg_integer(), fault: Macaw.Base.Fault.t() | nil, limited: boolean(), per_second: pos_integer(), tokens: non_neg_integer() }
A bucket: its rate and burst, the milli-events it holds, the clock's ms at its last
spend (nil before its first), the events it refused, whether its last spend was
refused, and the fault of the last one it refused.
Functions
@spec default() :: option()
The default option: 20 events a second, a burst of 40.
@spec faults(term()) :: [Macaw.Base.Fault.t()]
The faults of an event_budget: option, [] for a good one (spec/01 §2):
{[:event_budget], {:type, :event_budget}} for what is neither a keyword list nor
:unlimited, {[:event_budget, key], :unknown_key}, and {[:event_budget, key], {:type, :pos_integer}} for a rate or burst that isn't a positive integer.
iex> Macaw.Live.Visit.Budget.faults(per_second: 5)
[]
iex> Macaw.Live.Visit.Budget.faults(per_second: 0, every: 1)
[{[:event_budget, :every], :unknown_key}, {[:event_budget, :per_second], {:type, :pos_integer}}]
A full bucket for a judged option: nil for :unlimited, which spends nothing.
iex> Macaw.Live.Visit.Budget.new(burst: 2).tokens
2000
iex> Macaw.Live.Visit.Budget.new(:unlimited)
nil
@spec spend(t(), integer()) :: {:ok, t()} | {:error, Macaw.Base.Fault.t(), t()}
One event spent at the clock's ms: {:ok, bucket}, refilled by the milliseconds since
the last spend at per_second and one event taken, or {:error, fault, bucket} with the
event refused, counted and its fault kept. A clock read earlier than the last spend
refills nothing.
iex> bucket = Macaw.Live.Visit.Budget.new(per_second: 1, burst: 1)
iex> {:ok, bucket} = Macaw.Live.Visit.Budget.spend(bucket, 0)
iex> {:error, fault, bucket} = Macaw.Live.Visit.Budget.spend(bucket, 500)
iex> fault
{[:event], {:over_budget, %{per_second: 1, burst: 1}}}
iex> {:ok, bucket} = Macaw.Live.Visit.Budget.spend(bucket, 1_000)
iex> {bucket.dropped, bucket.limited}
{1, false}