# `Macaw.Live.Visit.Budget`
[🔗](https://github.com/dcoai/macaw_live/blob/v0.1.0/lib/macaw/live/visit/budget.ex#L1)

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 and `mount_page/3`'s `event_budget:`:
    `[per_second: n, burst: n]`, each a positive integer and each defaulting (`20` and
    `40`), 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 holds `burst` events and refills at `per_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}}}`, for `Macaw.Live.info/1`, and sets `@macaw.limited` so 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.

# `option`

```elixir
@type option() :: [per_second: pos_integer(), burst: pos_integer()] | :unlimited
```

The host's option: the rate and burst, either defaulting, or `:unlimited`.

# `t`

```elixir
@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.

# `default`

```elixir
@spec default() :: option()
```

The default option: 20 events a second, a burst of 40.

# `faults`

```elixir
@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}}]

# `new`

```elixir
@spec new(option()) :: t() | nil
```

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

# `spend`

```elixir
@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}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
