Beetle.make_rate_checker

You're seeing just the function make_rate_checker, go back to Beetle module for more information.
Link to this function

make_rate_checker(id_prefix, scale_ms, limit)

View Source

Specs

make_rate_checker(
  id_prefix :: String.t(),
  scale_ms :: integer(),
  limit :: integer()
) ::
  (id :: String.t() ->
     {:allow, count :: integer()}
     | {:deny, limit :: integer()}
     | {:error, reason :: any()})

Make a rate-checker function, with the given id prefix, scale_ms and limit.

Arguments:

  • id_prefix: String prefix to the id
  • scale_ms: Integer indicating size of bucket in milliseconds
  • limit: Integer maximum count of actions within the bucket

Returns a function which accepts an id suffix, which will be combined with the id_prefix. Calling this returned function is equivalent to: Beetle.check_rate("#{id_prefix}#{id}", scale_ms, limit)

Example:

chat_rate_limiter = make_rate_checker("send_chat_message:", 60_000, 20)
user_id = 203517
case chat_rate_limiter.(user_id) do
  {:allow, _count} ->
    # allow chat message
  {:deny, _limit} ->
    # deny
end
Link to this function

make_rate_checker(backend, id_prefix, scale_ms, limit)

View Source

Specs

make_rate_checker(
  backend :: atom(),
  id_prefix :: String.t(),
  scale_ms :: integer(),
  limit :: integer()
) ::
  (id :: String.t() ->
     {:allow, count :: integer()}
     | {:deny, limit :: integer()}
     | {:error, reason :: any()})