View Source Hammer.ETS (hammer v7.0.0-rc.3)

An ETS backend for Hammer.

To use the ETS backend, you need to start the process that creates and cleans the ETS table. The table is named after the module.

defmodule MyApp.RateLimit do
  use Hammer, backend: :ets
end

MyApp.RateLimit.start_link(clean_period: :timer.minutes(1))

Runtime configuration:

  • :clean_period - (in milliseconds) period to clean up expired entries, defaults to 1 minute
  • :key_older_than - (in milliseconds) maximum age for entries before they are cleaned up, defaults to 1 hour
  • :algorithm - the rate limiting algorithm to use, one of: :fix_window, :sliding_window, :leaky_bucket, :token_bucket. Defaults to :fix_window

Summary

Functions

Returns a specification to start this module under a supervisor.

Starts the process that creates and cleans the ETS table.

Types

@type config() :: %{
  table: atom(),
  table_opts: list(),
  clean_period: pos_integer(),
  key_older_than: pos_integer(),
  algorithm: module()
}
@type start_option() ::
  {:clean_period, pos_integer()}
  | {:table, atom()}
  | {:algorithm, module()}
  | {:key_older_than, pos_integer()}
  | GenServer.option()

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec now() :: pos_integer()
@spec start_link([start_option()]) :: GenServer.on_start()

Starts the process that creates and cleans the ETS table.

Accepts the following options:

  • :clean_period - How often to run the cleanup process (in milliseconds). Defaults to 1 minute.
  • :key_older_than - Optional maximum age for bucket entries (in milliseconds). Defaults to 24 hours. Entries older than this will be removed during cleanup.
  • :algorithm - The rate limiting algorithm to use. Can be :fixed_window, :sliding_window, :token_bucket, or :leaky_bucket. Defaults to :fixed_window.
  • optional :debug, :spawn_opts, and :hibernate_after GenServer options
Link to this function

update_counter(table, key, op, expires_at)

View Source