Throttlex v0.0.7 Throttlex View Source

Throttlex implements leaky bucket algorithm for rate limiting, it uses erlang ETS for storage.

Link to this section Summary

Functions

Clear given ets table, this is often needed in tests

Check user’s rate, same rate_per_second, max_accumulated should be passed to check functions in order to inspect user’s rate

Link to this section Functions

Link to this function clear(table) View Source
clear(atom | [atom]) :: :ok

Clear given ets table, this is often needed in tests

Link to this function do_check(table, id, rate_per_second, max_accumulated, cost) View Source
do_check(atom, integer | binary | tuple | atom, integer, integer, integer) ::
  :ok |
  :error

Check user’s rate, same rate_per_second, max_accumulated should be passed to check functions in order to inspect user’s rate.

##Arguments:

  • table: an atom representing bucket name.
  • id: id.
  • rate_per_second: how many rates should be added to bucket per second.
  • max_accumulated: maximum rates allowed in the bucket.
  • cost: costs of each request.

##Examples: # For user id 1, one extra request will be added to bucket, maximum accumulated requests number is 4, and every request will cost 1 token. First request will be permitted. iex> Throttlex.check(:user_request, 1, 1, 2, 1) :ok

# Second request is permitted also since we allowed 2 requests maximum. iex> Throttlex.check(:user_request, 1, 1, 2, 1) :ok

# If the third request is made within 1 second (the recovery time), it will return :error. iex> Throttlex.check(:user_request, 1, 1, 2, 1) :error