webrtc_rate_limit (webrtc v0.1.0)

View Source

Token bucket that holds one media stream to a bit rate, as a sans-IO engine: the limit a peer set with TMMBR (RFC 5104 §4.2.1.2, RFC 8834 §5.1.6).

  • Items are opaque, each with the number of bits it counts for. The budget grows by rate x elapsed time and is capped at burst_ms of the rate (at least one 1500 byte packet); an item leaves while the budget is positive and takes its bits from it. Over any interval the bits released are at most rate x interval plus the cap plus one item.
  • charge/3 takes bits sent outside the queue (FEC) from the budget.
  • The queue holds at most max_items; when full the oldest item is dropped. At rate 0 every item is dropped. Drops are counted.
  • With rate infinity items pass at once.
  • packet_rate/2 is the number of items released over the last second, for the limits that depend on it.

Times are integer milliseconds supplied by the caller.

Summary

Functions

Take Bits sent without the queue from the budget.

Queue an item that counts for Bits; returns the items that may leave now, in order.

Options: rate (bits per second or infinity, default infinity), burst_ms (default 40), max_items (default 256).

Items released over the last second, as a packet rate.

Change the rate; the budget earned so far is kept up to the new cap. Queued items are released by the next handle_timeout/2.

Types

state/0

-opaque state()

Functions

charge(Bits, Now, Rl)

-spec charge(non_neg_integer(), integer(), state()) -> state().

Take Bits sent without the queue from the budget.

dropped(Rl)

-spec dropped(state()) -> non_neg_integer().

enqueue(Item, Bits, Now, Rl)

-spec enqueue(term(), pos_integer(), integer(), state()) -> {[term()], state()}.

Queue an item that counts for Bits; returns the items that may leave now, in order.

handle_timeout(Now, Rl)

-spec handle_timeout(integer(), state()) -> {[term()], state()}.

new(Opts)

-spec new(map()) -> state().

Options: rate (bits per second or infinity, default infinity), burst_ms (default 40), max_items (default 256).

next_timeout(Rl)

-spec next_timeout(state()) -> integer() | infinity.

packet_rate(Now, Rl)

-spec packet_rate(integer(), state()) -> non_neg_integer().

Items released over the last second, as a packet rate.

queue_length(Rl)

-spec queue_length(state()) -> non_neg_integer().

rate(Rl)

-spec rate(state()) -> non_neg_integer() | infinity.

set_rate(Rate, Now, L)

-spec set_rate(non_neg_integer() | infinity, integer(), state()) -> state().

Change the rate; the budget earned so far is kept up to the new cap. Queued items are released by the next handle_timeout/2.