Kota.Bucket.DiscreteCounter (kota v0.1.4)

Copy Markdown View Source

A bucket adapter that gives an exact account of the taken allowances.

Every taken allowance is recorded with its own refill time, so an allowance becomes available again exactly :range_ms milliseconds after it was taken. This exactness comes at the expense of memory usage: the bucket keeps one entry per taken allowance until its refill time is reached.

Select this adapter with the :adapter option of Kota.start_link/1:

Kota.start_link(
  max_allow: 10,
  range_ms: 1000,
  adapter: Kota.Bucket.DiscreteCounter
)

Summary

Functions

Builds a new bucket from the given options.

Returns the time at which the next allowance will be refilled, in milliseconds.

Attempts to take one allowance from the bucket at the given time.

Functions

new(opts)

Builds a new bucket from the given options.

Requires the :max_allow and :range_ms options, both positive integers. Raises an ArgumentError when an option is missing or invalid.

next_refill!(map)

Returns the time at which the next allowance will be refilled, in milliseconds.

Raises when no refill is pending.

take(bucket, now)

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.