https://github.com/exhammer/hammer-backend-mnesia
Mnesia backend for Hammer
https://github.com/exhammer/hammer-backend-mnesia
elixir elixir-lang mnesia rate-limiter rate-limiting throttling
Last synced: 5 months ago
JSON representation
Mnesia backend for Hammer
- Host: GitHub
- URL: https://github.com/exhammer/hammer-backend-mnesia
- Owner: ExHammer
- License: mit
- Created: 2018-12-24T20:52:45.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-06-24T03:44:35.000Z (over 1 year ago)
- Last Synced: 2024-10-13T03:19:36.769Z (12 months ago)
- Topics: elixir, elixir-lang, mnesia, rate-limiter, rate-limiting, throttling
- Language: Elixir
- Homepage: https://hexdocs.pm/hammer-backend-mnesia
- Size: 60.5 KB
- Stars: 14
- Watchers: 3
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Hammer.Mnesia
[](https://github.com/ExHammer/hammer-backend-mnesia/actions/workflows/ci.yml)
[](https://hex.pm/packages/hammer_backend_mnesia)
[](https://hexdocs.pm/hammer_backend_mnesia)
[](https://hex.pm/packages/hammer_backend_mnesia)
[](https://github.com/ExHammer/hammer-backend-mnesia/blob/master/LICENSE.md)---
> [!NOTE]
>
> This README is for the unreleased master branch, please reference the [official documentation on hexdocs](https://hexdocs.pm/hammer_backend_mnesia/) for the latest stable release.---
A Mnesia backend for the [Hammer](https://github.com/ExHammer/hammer) rate-limiter.
This package is available in beta. If you have any problems, please open an issue.
> [!TIP]
> Consider using `Hammer.ETS` with counter increments broadcasted via [Phoenix PubSub](https://hexdocs.pm/phoenix_pubsub/Phoenix.PubSub.html) instead.## Installation
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.7.0"},
]
end
```## Usage
1. Define the rate limiter using `Hammer.Mnesia` backend:
```elixir
defmodule MyApp.RateLimit do
use Hammer, backend: Hammer.Mnesia
end
```2. Add the rate limiter to your supervision tree:
```elixir
children = [
# ...
{MyApp.RateLimit, clean_period: :timer.minutes(1)}
# ...
]
```3. And that's it, calls to the rate limiter will use Mnesia to store the counters.
```elixir
case MyApp.RateLimit.hit(key, _scale = :timer.minutes(1), _limit = 100) do
{:allow, _count} -> :ok
{:deny, retry_after} -> {:error, :rate_limit, "retry after #{retry_after}ms"}
end
```## Documentation
On hexdocs: [https://hexdocs.pm/hammer_backend_mnesia/](https://hexdocs.pm/hammer_backend_mnesia/)
## Getting Help
If you're having trouble, either open an issue on this repo, or reach out to the
maintainers ([@shanekilkelly](https://twitter.com/shanekilkelly)) on Twitter.