Throttlex v0.0.1 Throttlex View Source

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

Link to this section Summary

Functions

Check user’s rate, same rate_per_second, max_accumulated, cost should be passed to check functions in order to inspect user’s rate. And user id must be integer for efficiency issue

Link to this section Functions

Link to this function check(id, rate_per_second, max_accumulated, cost) View Source
check(integer, integer, integer, integer) :: :ok | :error

Check user’s rate, same rate_per_second, max_accumulated, cost should be passed to check functions in order to inspect user’s rate. And user id must be integer for efficiency issue.

##Arguments:

  • 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(1, 1, 2, 1) :ok

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

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