limitex v0.1.1 Limitex.RateLimiter View Source

A distributed rate limiter for expensive requests based on Token Bucket algorithm.

Link to this section Summary

Functions

Check if the action you wish to perform is within the bounds of the rate-limit.

Returns a specification to start this module under a supervisor.

Link to this section Types

Link to this type

bucket_info()

View Source
bucket_info() ::
  {key :: bucket_key(), count :: integer(), created :: integer(),
   updated :: integer()}
Link to this type

bucket_key()

View Source
bucket_key() :: {bucket :: integer(), id :: String.t()}

Link to this section Functions

Link to this function

check_rate(id, scale_ms, limit, increment \\ 1)

View Source
check_rate(String.t(), integer(), integer(), integer()) ::
  {:ok, integer()} | {:error, :rate_limited} | {:error, :internal_server_error}

Check if the action you wish to perform is within the bounds of the rate-limit.

  • id: String name of the bucket. Usually comprised of a fixed prefix and some dynamic string appended
  • scale_ms: Integer indicating size of bucket in milliseconds
  • limit: Integer maximum count of actions within the bucket

Example:

user_id = 42076
case check_rate("file_upload:#{user_id}", 60_000, 5) do
  {:ok, _count} ->
    # do the file upload
  {:error, :rate_limited} ->
    # render an error page or something
end

Returns a specification to start this module under a supervisor.

See Supervisor.