Skip to content
forked from ExHammer/hammer

An Elixir rate-limiter with pluggable backends

License

Notifications You must be signed in to change notification settings

ni-kismet/hammer

 
 

Repository files navigation

Hammer

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

Build Status

Coverage Status

(Note: Hammer API has changed in v2.0.0, see documentation)

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, "~> 3.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)
  • 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

Acknowledgements

Hammer was inspired by the ExRated library, by grempe.

About

An Elixir rate-limiter with pluggable backends

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Elixir 98.8%
  • Makefile 1.2%