View Source Upgrading to Hammer V7
Elixir and Erlang/OTP Compatibility
- Hammer v7 requires Elixir 1.14 and Erlang/OTP 25 at a minimum.
- We recommend using the latest Elixir and Erlang/OTP versions.
Changes to your Project
- Update your
mix.exsto depend on version7.0.0of Hammer.
def deps do
[
...
{:hammer, "~> 7.0.0"}
...
]
endDefine a Rate Limiter
First, define a rate limiter module in your application. Use the Hammer module with your chosen backend and configure options as needed:
defmodule MyApp.RateLimit do
use Hammer, backend: :ets
endThis would setup the rate limiter using the Hammer.ETS backend. See the Tutorial guide for more information on other backends.
Update your Application Supervisor
- Pick up the value in your config file for
cleanup_interval_ms. - remove the
configlines forHammeras they are no longer needed in all of theconfig/*.exsfiles. - In your
application.exfile, add the following line to start the rate limiter:
def start(_type, _args) do
children = [
...
{MyApp.RateLimit, [clean_period: 60_000]}
...
]
Supervisor.start_link(children, strategy: :one_for_one)
endChange to Backend Configuration
We have simplified the backend API. Hammer.inc/4 combines the functionality of Hammer.check_rate and Hammer.check_rate_inc now.
- Remapped all the
Hammer.check_rate/3andHammer.check_rate/4toHammer.inc/4. - Remapped all the
Hammer.check_rate_inc/4andHammer.check_rate_inc/5toHammer.inc/4. - for the
Hammer.delete_buckets, you need to remove them as there no true replacement. You could potentially useHammer.ETS.set/1to reset specific key - for the
Hammer.make_rate_checker, you need to remove them as there no replacement.
Changes to the Hammer.Plug
- The
Hammer.Plughas been removed. Remove any references to it in your code. - Migrate it by using regular Phoenix plugs in either a controller plug or an endpoint plug. See the Tutorial guide for some examples.