The Wayback Machine - https://web.archive.org/web/20191227075941/https://github.com/ExHammer/hammer
Skip to content
An Elixir rate-limiter with pluggable backends
Elixir Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config New Api, again. Spawn the backend supervisor under the hammer app Sep 9, 2017
doc_src Add Mnesia link to frontpage of docs Jan 3, 2019
lib Warn about using ETS in production Oct 13, 2018
logo
test Insist on ETS configuration Sep 13, 2018
.editorconfig Adds credo and style fixes Aug 9, 2017
.gitignore init Jun 1, 2017
.travis.yml Don't do dialyzer by default in CI Jul 24, 2018
CHANGELOG.md Insist on ETS configuration Sep 13, 2018
LICENSE.txt Add MIT license. Jul 23, 2017
Makefile Add dialyzer support Jul 23, 2018
README.md
coveralls.json add coveralls config file Sep 12, 2017
mix.exs
mix.lock WIP: use a worker-pool Mar 27, 2018

README.md

hammer

Hammer

A rate-limiter for Elixir, with pluggable storage backends.

Build Status

Coverage Status

New! Hammer-Plug

We've just released a new helper-library to make adding rate-limiting to your Phoenix (or other plug-based) application even easier: Hammer.Plug.

Installation

Hammer is available in Hex, the package can be installed by adding hammer to your list of dependencies in mix.exs:

def deps do
  [{:hammer, "~> 6.0"}]
end

Documentation

On hexdocs: https://hexdocs.pm/hammer/frontpage.html

The Tutorial is an especially good place to start.

Usage

Example:

defmodule MyApp.VideoUpload do

  def upload(video_data, user_id) do
    case Hammer.check_rate("upload_video:#{user_id}", 60_000, 5) do
      {:allow, _count} ->
        # upload the video, somehow
      {:deny, _limit} ->
        # deny the request
    end
  end

end

The Hammer module provides the following functions:

  • check_rate(id, scale_ms, limit)
  • check_rate_inc(id, scale_ms, limit, increment)
  • inspect_bucket(id, scale_ms, limit)
  • delete_buckets(id)

Backends are configured via Mix.Config:

config :hammer,
  backend: {Hammer.Backend.ETS, [expiry_ms: 60_000 * 60 * 4,
                                 cleanup_interval_ms: 60_000 * 10]}

See the Tutorial for more.

See the Hammer Testbed app for an example of using Hammer in a Phoenix application.

Available Backends

Getting Help

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

Acknowledgements

Hammer was inspired by the ExRated library, by grempe.

You can’t perform that action at this time.