DistLimiter (dist_limiter v0.1.0) View Source

Link to this section Summary

Functions

Consume a count of resources if available.

Get remaining count of the given resource.

Link to this section Functions

Link to this function

consume(resource, rate, count)

View Source

Specs

consume(
  resource :: any(),
  {window :: integer(), limit :: integer()},
  count :: integer()
) :: {:ok, integer()} | {:error, :overflow}

Consume a count of resources if available.

If succeeds, {:ok, remaining_count}.

If falied, {:error, :overflow}.

  iex> DistLimiter.consume({:ip, "a.b.c.d", :password_challenge}, {60000, 1}, 1)
  {:ok, 0}
  iex> DistLimiter.consume({:ip, "a.b.c.d", :password_challenge}, {60000, 1}, 1)
  {:error, :overflow}
Link to this function

get_remaining(resource, rate)

View Source

Specs

get_remaining(resource :: any(), {window :: integer(), limit :: integer()}) ::
  integer()

Get remaining count of the given resource.

  iex> DistLimiter.get_remaining({:ip, "a.b.c.d", :password_challenge}, {60000, 1})
  1
  iex> DistLimiter.consume({:ip, "a.b.c.d", :password_challenge}, {60000, 1}, 1)
  {:ok, 0}
  iex> DistLimiter.get_remaining({:ip, "a.b.c.d", :password_challenge}, {60000, 1})
  0