View Source Hammer.Backend.ETS (hammer v6.2.1)

An ETS backend for Hammer.

The public API of this module is used by Hammer to store information about rate-limit 'buckets'. A bucket is identified by a key, which is a tuple {bucket_number, id}. The essential schema of a bucket is: {key, count, created_at, updated_at}, although backends are free to store and retrieve this data in whichever way they wish.

Use start or start_link to start the server:

{:ok, pid} = Hammer.Backend.ETS.start_link(args)

args is a keyword list:

  • expiry_ms: (integer) time in ms before a bucket is auto-deleted, should be larger than the expected largest size/duration of a bucket
  • cleanup_interval_ms: (integer) time between cleanup runs,
  • ets_table_type: (atom) type of ETS table, defaults to :set, and can be either :set or :ordered_set

Example:

Hammer.Backend.ETS.start_link(
  expiry_ms: 1000 * 60 * 60,
  cleanup_interval_ms: 1000 * 60 * 10
)

Summary

Functions

Returns a specification to start this module under a supervisor.

Record a hit in the bucket identified by key

Record a hit in the bucket identified by key, with a custom increment

Delete all buckets associated with id.

Retrieve information about the bucket identified by key

Callback implementation for GenServer.init/1.

Types

@type bucket_info() ::
  {key :: bucket_key(), count :: integer(), created :: integer(),
   updated :: integer()}
@type bucket_key() :: {bucket :: integer(), id :: String.t()}

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

count_hit(pid, key, now)

View Source
@spec count_hit(
  pid :: pid(),
  key :: bucket_key(),
  now :: integer()
) :: {:ok, count :: integer()} | {:error, reason :: any()}

Record a hit in the bucket identified by key

Link to this function

count_hit(pid, key, now, increment)

View Source
@spec count_hit(
  pid :: pid(),
  key :: bucket_key(),
  now :: integer(),
  increment :: integer()
) :: {:ok, count :: integer()} | {:error, reason :: any()}

Record a hit in the bucket identified by key, with a custom increment

@spec delete_buckets(
  pid :: pid(),
  id :: String.t()
) :: {:ok, count_deleted :: integer()} | {:error, reason :: any()}

Delete all buckets associated with id.

@spec get_bucket(
  pid :: pid(),
  key :: bucket_key()
) :: {:ok, info :: bucket_info()} | {:ok, nil} | {:error, reason :: any()}

Retrieve information about the bucket identified by key

Callback implementation for GenServer.init/1.