https://github.com/jechol/dist_limiter
Distributed rate limiter
https://github.com/jechol/dist_limiter
Last synced: about 1 year ago
JSON representation
Distributed rate limiter
- Host: GitHub
- URL: https://github.com/jechol/dist_limiter
- Owner: jechol
- License: other
- Created: 2020-12-08T16:56:51.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-11T05:57:19.000Z (about 2 years ago)
- Last Synced: 2025-02-15T07:12:14.743Z (over 1 year ago)
- Language: Elixir
- Homepage:
- Size: 37.1 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://github.com/jechol/dist_limiter/actions)
[](https://hex.pm/packages/dist_limiter)
[](https://github.com/jechol/dist_limiter/blob/master/LICENSE.md)
# DistLimiter
Distributed rate limiter.
## Features
### Distributed
Built on top of `pg`, nodes which are interested in the same resource automatically form a process group. They record local `consume` timestamps and exchange those records with other nodes when being asked to `consume`.
### Garbage collection
Member processes automatically stop when `window` milliseconds elapsed after last `consume` on the node.
### Sliding window algorithm
`dist_limiter` implemented sliding window algorithm.
## Usage
Let's say we want to limit password challenge from IP "a.b.c.d" to at most 3 times a hour.
```elixir
iex(1)> resource = {:ip, "a.b.c.d", :challenge_password}
{:ip, "a.b.c.d", :challenge_password}
iex(2)> limit = {3600 * 1000, 3}
{3600000, 3}
iex(3)> DistLimiter.get_remaining(resource, limit)
3
iex(4)> DistLimiter.consume(resource, limit, 1)
{:ok, 2}
iex(5)> DistLimiter.consume(resource, limit, 1)
{:ok, 1}
iex(6)> DistLimiter.consume(resource, limit, 1)
{:ok, 0}
iex(7)> DistLimiter.consume(resource, limit, 1)
{:error, :overflow}
```
## Installation
Add `dist_limiter` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:dist_limiter, "~> 0.1.1"}
]
end
```