A bucket adapter that accounts for taken allowances in time slots.
Taken allowances are grouped into slots of :slot_ms milliseconds, and each
slot's usage is refilled as a whole, :range_ms milliseconds after the last
allowance taken in the slot. Grouping keeps memory usage and processing low
compared to Kota.Bucket.DiscreteCounter: the bucket tracks one refill per
slot instead of one per taken allowance.
Because a whole slot is refilled at once, allowances taken during a slot are refilled as if they had all been taken at the same time as the last one. Refills happen later than with an exact per-allowance account, so under constant load this results in a slightly lower effective rate than the configured maximum.
Select this adapter with the :adapter option of Kota.start_link/1:
Kota.start_link(
max_allow: 100,
range_ms: 1000,
slot_ms: 100,
adapter: Kota.Bucket.SlidingWindow
)
Summary
Functions
Builds a new bucket from the given options.
Returns the time at which the next slot will be refilled, in milliseconds.
Attempts to take one allowance from the bucket at the given time.
Functions
Builds a new bucket from the given options.
Requires the :max_allow, :range_ms and :start_time options. The
:slot_ms option is a positive integer of milliseconds, at most equal to
:range_ms, or :one_tenth to use a tenth of :range_ms. Using
:one_tenth requires :range_ms to be at least 10. Raises an
ArgumentError when an option is missing or invalid.
Returns the time at which the next slot will be refilled, in milliseconds.
Raises when no refill is pending.
Attempts to take one allowance from the bucket at the given time.
Returns {:ok, bucket} when an allowance is available, or {:reject, bucket}
when the bucket is exhausted for the current time. The now argument is the
current time in milliseconds, as given by Kota.now_ms/0.