Note
This README is for the unreleased master branch, please reference the official documentation on hexdocs for the latest stable release.
A Mnesia backend for the Hammer rate-limiter.
This package is available in beta. If you have any problems, please open an issue.
Tip
Consider using Hammer.ETS with counter increments broadcasted via Phoenix PubSub instead.
The package can be installed by adding hammer_backend_mnesia to your list of dependencies in mix.exs:
def deps do
[
{:hammer_backend_mnesia, "~> 0.7.0"},
]
end-
Define the rate limiter using
Hammer.Mnesiabackend:defmodule MyApp.RateLimit do use Hammer, backend: Hammer.Mnesia end
-
Add the rate limiter to your supervision tree:
children = [ # ... {MyApp.RateLimit, clean_period: :timer.minutes(1)} # ... ]
-
And that's it, calls to the rate limiter will use Mnesia to store the counters.
case MyApp.RateLimit.hit(key, _scale = :timer.minutes(1), _limit = 100) do {:allow, _count} -> :ok {:deny, retry_after} -> {:error, :rate_limit, "retry after #{retry_after}ms"} end
On hexdocs: https://hexdocs.pm/hammer_backend_mnesia/
If you're having trouble, either open an issue on this repo, or reach out to the maintainers (@shanekilkelly) on Twitter.