Skip to content

Commit

Permalink
new hammer
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Nov 15, 2024
1 parent 5426716 commit b6372f7
Show file tree
Hide file tree
Showing 15 changed files with 384 additions and 504 deletions.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["mix.exs", "{lib,test}/**/*.{ex,exs}"]
]
47 changes: 25 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,57 @@ name: CI

on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
setup:
test:
runs-on: ubuntu-latest

env:
MIX_ENV: test

strategy:
fail-fast: false
# https://hexdocs.pm/elixir/compatibility-and-deprecations.html#between-elixir-and-erlang-otp
matrix:
os: [ubuntu-22.04, ubuntu-20.04]
elixir_version: [1.12.3, 1.13.3, 1.14.1]
otp_version: [24, 25]
exclude:
- otp_version: 25
elixir_version: 1.12.3
elixir: [1.15, 1.16, 1.17]
otp: [25, 26]
include:
- elixir: 1.17
otp: 27

steps:
- uses: actions/checkout@v4

- uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.otp_version}}
elixir-version: ${{matrix.elixir_version}}
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}

- uses: actions/cache@v4
with:
path: |
deps
_build
key: deps-${{ runner.os }}-${{ matrix.otp_version }}-${{ matrix.elixir_version }}-${{ hashFiles('**/mix.lock') }}
key: test-otp-${{ matrix.otp }}-elixir-${{ matrix.elixir }}-ref-${{ github.head_ref || github.ref }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: |
deps-${{ runner.os }}-${{ matrix.otp_version }}-${{ matrix.elixir_version }}
- run: mix deps.get

- run: mix format --check-formatted
test-otp-${{ matrix.otp }}-elixir-${{ matrix.elixir }}-ref-${{ github.head_ref || github.ref }}-mix-
test-otp-${{ matrix.otp }}-elixir-${{ matrix.elixir }}-ref-refs/heads/master-mix-
- run: mix deps.get --only $MIX_ENV
- run: mix deps.unlock --check-unused

- run: mix deps.compile

- run: mix compile --warnings-as-errors

- run: mix credo --strict --format=oneline
- run: mix test --warnings-as-errors --cover --include slow

- run: mix test --warnings-as-errors --cover
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
elixir-version: 1
otp-version: 27
- run: mix format --check-formatted
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ erl_crash.dump

# Ignore package tarball (built via "mix hex.build").
hammer_backend_mnesia-*.tar

Mnesia.nonode@nohost/
1 change: 0 additions & 1 deletion .tool-versions

This file was deleted.

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.7.0 - Unreleased

- adapt to the new Hammer API

## 0.6.1 - 2024-03-29

Expand Down
58 changes: 27 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,56 @@
# Hammer.Backend.Mnesia
# Hammer.Mnesia

[![Build Status](https://github.com/ExHammer/hammer-backend-mnesia/actions/workflows/ci.yml/badge.svg)](https://github.com/ExHammer/hammer-backend-mnesia/actions/workflows/ci.yml) [![Hex.pm](https://img.shields.io/hexpm/v/hammer_backend_mnesia.svg)](https://hex.pm/packages/hammer_backend_mnesia) [![Documentation](https://img.shields.io/badge/documentation-gray)](https://hexdocs.pm/hammer_backend_mnesia)
[![Build Status](https://github.com/ExHammer/hammer-backend-mnesia/actions/workflows/ci.yml/badge.svg)](https://github.com/ExHammer/hammer-backend-mnesia/actions/workflows/ci.yml)
[![Hex.pm](https://img.shields.io/hexpm/v/hammer_backend_mnesia.svg)](https://hex.pm/packages/hammer_backend_mnesia)
[![Documentation](https://img.shields.io/badge/documentation-gray)](https://hexdocs.pm/hammer_backend_mnesia)
[![Total Download](https://img.shields.io/hexpm/dt/hammer_backend_mnesia.svg)](https://hex.pm/packages/hammer_backend_mnesia)
[![License](https://img.shields.io/hexpm/l/hammer_backend_mnesia.svg)](https://github.com/ExHammer/hammer-backend-mnesia/blob/master/LICENSE.md)

An Mnesia backend for the [Hammer](https://github.com/ExHammer/hammer)
rate-limiter.
A Mnesia backend for the [Hammer](https://github.com/ExHammer/hammer) rate-limiter. By default it uses a single `type: :set` in-memory Mnesia table that is not distributed. See Mnesia documentation for [`create_table/2`](https://www.erlang.org/doc/apps/mnesia/mnesia.html#create_table/2) for more information.

This package is available in beta. If you have any problems, please open an issue.

> [!TIP]
> Consider using ETS tables with counter increments broadcasted over Phoenix.PubSub instead. That approach is both more performant and less error prone.
## Installation

Hammer-backend-mnesia
is [available in Hex](https://hex.pm/packages/hammer_backend_mnesia), the package
can be installed by adding `hammer_backend_mnesia` to your list of dependencies in `mix.exs`:

The package can be installed by adding `hammer_backend_mnesia` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:hammer_backend_mnesia, "~> 0.6"},
{:hammer, "~> 6.1"}]
[
{:hammer_backend_mnesia, "~> 0.7.0"},
]
end
```


## Usage

First, set up an Mnesia schema, see this guide: https://elixirschool.com/en/lessons/specifics/mnesia/

Then, create the Mnesia table for Hammer to use:

```elixir
Hammer.Backend.Mnesia.create_mnesia_table()
```

Configure the `:hammer` application to use the Mnesia backend:
1. Define the rate limiter using `Hammer.Mnesia` backend:

```elixir
config :hammer,
backend: {Hammer.Backend.Mnesia, [expiry_ms: 60_000 * 60 * 2,
cleanup_interval_ms: 60_000 * 10]}
```
```elixir
defmodule MyApp.RateLimit do
use Hammer, backend: Hammer.Mnesia
end
```

And that's it, calls to `Hammer.check_rate/3` and so on will use Mnesia to store
the rate-limit counters.
2. Add the rate limiter to your supervision tree:

See the [Hammer Tutorial](https://hexdocs.pm/hammer/tutorial.html) for more.
```elixir
children = [
# you can add `ram_copies: Node.list()` to make the table distributed, but that requires extra configuration
{MyApp.RateLimit, clean_period: :timer.minutes(1)}
]
```

Note that this process will fail to start if `:mnesia.create_table/2` call fails. Depending on your supervision strategy that can take down the whole application. So be careful with the extra options you provide.

3. And that's it, calls to `MyApp.RateLimit.hit/3` and so on will use Mnesia to store the rate-limit counters.
## Documentation
On hexdocs:
[https://hexdocs.pm/hammer_backend_mnesia/](https://hexdocs.pm/hammer_backend_mnesia/)

On hexdocs: [https://hexdocs.pm/hammer_backend_mnesia/](https://hexdocs.pm/hammer_backend_mnesia/)
## Getting Help
Expand Down
39 changes: 0 additions & 39 deletions config/config.exs

This file was deleted.

33 changes: 0 additions & 33 deletions guides/overview.md

This file was deleted.

Loading

0 comments on commit b6372f7

Please sign in to comment.