Hammer.Redis (hammer_backend_redis v7.0.2)

View Source

This backend uses the Redix library to connect to Redis.

defmodule MyApp.RateLimit do
  # the default prefix is "MyApp.RateLimit:"
  # the default timeout is :infinity
  use Hammer, backend: Hammer.Redis, prefix: "MyApp.RateLimit:", timeout: :infinity
end

MyApp.RateLimit.start_link(url: "redis://localhost:6379")

# increment and timeout arguments are optional
# by default increment is 1 and timeout is as defined in the module
{:allow, _count} = MyApp.RateLimit.hit(key, scale, limit)
{:allow, _count} = MyApp.RateLimit.hit(key, scale, limit, _increment = 1, _timeout = :infinity)

The Redis backend supports the following algorithms:

  • :fix_window - Fixed window rate limiting (default) Simple counting within fixed time windows. See Hammer.Redis.FixWindow for more details.

  • :leaky_bucket - Leaky bucket rate limiting Smooth rate limiting with a fixed rate of tokens. See Hammer.Redis.LeakyBucket for more details.

  • :token_bucket - Token bucket rate limiting Flexible rate limiting with bursting capability. See Hammer.Redis.TokenBucket for more details.

Summary

Types

redis_option()

@type redis_option() :: {:url, String.t()} | {:name, String.t()}

redis_options()

@type redis_options() :: [redis_option()]