Hammer.Mnesia (hammer_backend_mnesia v0.7.0)

View Source

This backend uses the :mnesia.

defmodule MyApp.RateLimit do
  # the default table is the current's module name
  use Hammer, backend: Hammer.Mnesia, table: __MODULE__
end

# attempts to create a distributed in-memory table and run the clean up every 10 minutes
MyApp.RateLimit.start_link(clean_period: :timer.minutes(10))

{:allow, _count} = MyApp.RateLimit.hit(key, scale, limit)

The Mnesia backend supports the following algorithms:

  • :fix_window - Fixed window rate limiting (default) Simple counting within fixed time windows. See Hammer.Mnesia.FixWindow for more details.

  • :leaky_bucket - Leaky bucket rate limiting Smooth rate limiting with a fixed rate of tokens. See Hammer.Mnesia.LeakyBucket for more details.

  • :token_bucket - Token bucket rate limiting Flexible rate limiting with bursting capability. See Hammer.Mnesia.TokenBucket for more details.

Summary

Functions

Returns a specification to start this module under a supervisor.

Starts the process that creates and cleans the ETS table.

Types

mnesia_option()

@type mnesia_option() :: {:table, atom()}

mnesia_options()

@type mnesia_options() :: [mnesia_option()]

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

now()

start_link(opts)

@spec start_link(mnesia_options()) :: {:ok, pid()} | :ignore | {:error, term()}

Starts the process that creates and cleans the ETS table.

Accepts the following options:

  • :clean_period for how often to perform garbage collection
  • optional mnesia options from :mnesia.create_table/2
  • :key_older_than - How old a key can be before it is removed from the table (in milliseconds). Defaults to 10 minutes.
  • :algorithm - The rate limiting algorithm to use. Can be :fixed_window, :token_bucket, or :leaky_bucket. Defaults to :fixed_window.
  • optional :debug, :spawn_opts, and :hibernate_after GenServer.options()