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

sobolevn/hammer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hammer

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

Build Status

Coverage Status

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, "~> 0.1.0"}]
end

Documentation

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

The Tutorial is an especially good place to start.

Usage

To use Hammer, you need to do two things:

  • Start a backend process
  • use the Hammer module

The example below combines both in a MyApp.RateLimiter module:

defmodule MyApp.RateLimiter do
  use Supervisor
  use Hammer, backend: Hammer.Backend.ETS

  def start_link() do
    Supervisor.start_link(__MODULE__, :ok)
  end

  def init(:ok) do
    children = [
      worker(Hammer.Backend.ETS, [[expiry_ms: 1000 * 60 * 60
                                   cleanup_interval_ms: 1000 * 60 * 10]]),
    ]
    supervise(children, strategy: :one_for_one, name: MyApp.RateLimiter)
  end
end

The Hammer module provides the following functions (via use):

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

The rate-limiter is then used in the app by calling the check_rate function:

defmodule MyApp.VideoUpload do

  alias MyApp.RateLimiter

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

end

See the Tutorial for more.

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

Available Backends

About

An Elixir rate-limiter with pluggable backends

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Elixir 99.8%
  • Makefile 0.2%