Hammer.Mnesia

View Source

Build Status Hex.pm Documentation Total Download License


[!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.

Installation

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

Usage

  1. Define the rate limiter using Hammer.Mnesia backend:

     defmodule MyApp.RateLimit do
       use Hammer, backend: Hammer.Mnesia
     end
  2. Add the rate limiter to your supervision tree:

     children = [
       # ...
       {MyApp.RateLimit, clean_period: :timer.minutes(1)}
       # ...
     ]
  3. 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

Documentation

On hexdocs: https://hexdocs.pm/hammer_backend_mnesia/

Getting Help

If you're having trouble, either open an issue on this repo, or reach out to the maintainers (@shanekilkelly) on Twitter.