Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/4xposed/minirate
A dead simple distributed rate limiting library in Elixir using Mnesia.
https://github.com/4xposed/minirate
distributed elixir mnesia plug rate-limiter rate-limiting
Last synced: about 1 month ago
JSON representation
A dead simple distributed rate limiting library in Elixir using Mnesia.
- Host: GitHub
- URL: https://github.com/4xposed/minirate
- Owner: 4xposed
- License: mit
- Created: 2020-02-01T20:24:52.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-01-23T10:31:25.000Z (11 months ago)
- Last Synced: 2024-04-24T12:25:14.349Z (8 months ago)
- Topics: distributed, elixir, mnesia, plug, rate-limiter, rate-limiting
- Language: Elixir
- Homepage:
- Size: 30.3 KB
- Stars: 12
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![Elixir CI](https://github.com/4xposed/minirate/workflows/Elixir%20CI/badge.svg?event=push)
# Minirate
A dead simple distributed rate limiting library in Elixir using Mnesia.
# What is it?
A distributed rate limiter with a focus on readable and well tested code.
The counter is syncronized over all connected nodes
```elixir
iex([email protected])19> Minirate.check_limit("download", "user_1", 100)
{:allow, 1}
```
```elixir
iex([email protected])14> Minirate.check_limit("download", "user_1", 100)
{:allow, 2}
```## Installation
Minirate is availabe as a package in Hex, just add it to your `mix.exs` file:
```elixir
def deps
[{:minirate, "~> 0.1"}]
end
```and add it to your extra applications:
```elixir
def applications do
[
extra_applications: [:minirate]
]
```## Configuration
Minirate needs to be configured using Mix.Config.
For example, in `config/config.exs`:
```
config :minirate,
mnesia_table: :rate_limiter,
expiry_ms: 60_000
cleanup_period_ms: 10_000
````mnesia_table` specifies which table will Mnesia use to write the counters.
`expiry_ms` specifies the counter life in millisecconds (for example to have rates like x request every 10 seconds, you would set `expiry_ms` to 10_000)
`cleanup_period_ms` specifies how often minirate will clean expired counters from the mnesia database## Usage
With Minirate you can rate limit any action on your application.
The module `Minirate` the function `check_limit(action_name, identifier, limit)`
An Example:
```elixir
@download_limit 1_000def download_file(file, user_id) do
case Minirate.check_limit("download_file", user_id, @download_limit) do
{:allow, _count} ->
# Logic to download the file{:block, _reason} ->
# Logic when the limit has been reached{:skip, _reason} ->
# Skip will only happen if there's a problem with your nodes or mnesia setup and a count cannot be determined.
end
```## Using Minirate.Plug
`Minirate.Plug` can rate-limit actions in your web application using the ip address of the requester.
You can just put in the pipeline of your web application something like this:
```elixir
plug Minirate.Plug, [action: action, limit: 10_000]
```or for more flexibilty:
```elixir
plug Minirate.Plug, [action: "custom_action", limit: 10_000] when action == :update or action == :create
```