Skip to content

joeychilson/simple_rate_limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleRateLimiter

SimpleRateLimiter is a simple rate limiter library for Elixir. It allows you to limit the number of actions (e.g., API requests, database calls, etc.) that can be performed within a specific time interval. It is particularly useful when dealing with APIs that have rate limit restrictions, and you want to ensure your application stays within those limits.

Installation

SimpleRateLimiter is available on Hex. Add it to your list of dependencies in mix.exs:

def deps do
  [
    {:simple_rate_limiter, "~> 1.0.0"}
  ]
end

Usage

Add SimpleRateLimiter to your supervision tree, and specify the interval and max options. The interval option specifies the time interval in milliseconds, and the max option specifies the maximum number of actions that can be performed within that time interval. For example, if you want to limit the number of API requests to 10 per second, you would specify interval: 1_000 and max: 10.

defmodule APIClient do
  use Application

  def start(_type, _args) do
    children = [
      {SimpleRateLimiter, interval: 1_000, max: 10}
    ]

    opts = [strategy: :one_for_one, name: APIClient.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Then, in your application code, you can call SimpleRateLimiter.wait_and_proceed/1 with a function that performs the action you want to limit. For example, if you want to limit the number of API requests to 10 per second, you would do something like this:

defmodule APIClient.Client do
  def get(url, headers) do
    SimpleRateLimiter.wait_and_proceed(fn -> HTTPoison.get(url, headers) end)
  end
end

About

A simple rate limiter for Elixir.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages