View Source Hammer.EredisCluster (hammer_backend_eredis_cluster v2.0.0)
A Hammer (v7+) backend for clustered Redis, built on eredis_cluster.
defmodule MyApp.RateLimit do
use Hammer, backend: Hammer.EredisCluster
end
# The cluster is connected elsewhere (e.g. the :eredis_cluster app env
# `init_nodes`) — just start the limiter:
MyApp.RateLimit.start_link([])
# Or let the limiter establish the connection itself:
MyApp.RateLimit.start_link(
servers: [{"127.0.0.1", 7001}, {"127.0.0.1", 7002}],
options: [username: "myuser", password: "mypassword"]
)
{:allow, _count} = MyApp.RateLimit.hit("user_123", _scale_ms = :timer.minutes(1), _limit = 10)
Compile-time options (use Hammer, backend: Hammer.EredisCluster, ...)
:cluster— the eredis_cluster name to run commands against (default:eredis_cluster_default, eredis_cluster's default cluster).:prefix— Redis key prefix (default: the limiter module name).:algorithm— only:fix_windowis supported.
Runtime options (start_link/1)
:servers— list of{host, port}seed nodes. Omit when the cluster connection is managed elsewhere (e.g.config :eredis_cluster, init_nodes: ...).:options— keyword list forwarded to eredis (:username,:password, ...).
Keys
Redis keys are prefix:{key}:window. The {key} hash tag pins every window
of a given key to the same cluster slot, preserving slot affinity for
multi-window operations. Keys must be strings — this backend serializes them
into Redis key names (v7.1's term() keys are not supported here).
Unlike the v6 backend (Hammer.Backend.EredisCluster), there is no process
in the request path: hit/3,4 and friends talk to eredis_cluster's pools
directly. The process started by start_link/1 only establishes the cluster
connection and holds no per-request state.
Summary
Functions
Returns a specification to start this module under a supervisor.
Types
@type option() :: {:cluster, atom()} | {:servers, [server()]} | {:options, Keyword.t()} | {:name, GenServer.name()}
@type server() :: {host :: String.t() | charlist(), port :: :inet.port_number()}
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.