View Source Hammer.Backend.Mnesia (hammer_backend_mnesia v0.6.1)

An Mnesia 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.Mnesia.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,
  • table_name: (atom) table name to use, defaults to :__hammer_backend_mnesia,

Example:

Hammer.Backend.Mnesia.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

Create the mnesia table.

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

Link to this function

create_mnesia_table(table_name)

View Source
Link to this function

create_mnesia_table(table_name, opts)

View Source

Create the mnesia table.

  • table_name: atom name of table, defaults to :__hammer_backend_mnesia
  • opt: keyword list of options to :mnesia.create_table/2, all options are suppoted except :access_mode, :attributes, :index, :type, and :record_name, which are not configurable.
@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.