Coder Social home page Coder Social logo

simple_rate_limiter's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.