Giter Club home page Giter Club logo

leaky's Introduction

Leaky

Implements a token-based rate limiter using the leaky bucket algorithm, ideal for controlling access rates to resources in Elixir applications. This implementation leverages ETS for optimized performance, making it suitable for high-load environments.

Overview

The leaky bucket algorithm helps smooth out bursty traffic by limiting the rate at which actions are taken. It's like a bucket with a hole: tokens, representing permissions to take an action, drip out at a constant rate. If the bucket is full, new actions must wait, ensuring the overall rate does not exceed the desired threshold. Read more about the algorithm here: https://en.wikipedia.org/wiki/Leaky_bucket

Instalation

Add the following to your mix.exs file:

  def deps do
  [
    {:leaky, "~> 0.1.1"},
  ]
end

Usage

The rate limiter process can be customized with several options:

  • bucket_name: Unique identifier for the bucket.
  • max_accumulated: The maximum tokens the bucket can hold. Once full, new tokens will not accumulate.
  • refill: The number of tokens added to the bucket on each refill cycle.
  • interval: The time (in ms) between each refill cycle. Defaults to 1000 ms.
  • name: The GenServer process name. Defaults to Leaky.
  configuration = [bucket_name: :user_requests, max_accumulated: 100, refill: 10, interval: 1000]
  {:ok, _pid} = Leaky.start_link(configuration)

To attempt an action, checking if it's allowed under the current rate:

  case Leaky.acquire(:user_requests, 1) do
    {:allow, tokens_left} -> "Action allowed."
    :deny ->"Action denied."
  end

leaky's People

Contributors

ihorkatkov avatar

Stargazers

 avatar Yurii Zhyvaha avatar  avatar Olexandr Maslo avatar

Watchers

 avatar

leaky's Issues

Add possibility to use adapters

Despite the library is useful in most cases, it lacks extensibility in terms of possible backends. Consider adding backend behaviour for different backends (including using Nebulex)

Cache dialyzer PLTs

That's needed to improve CI speed execution. The GitHub workflow's configuration should be extended with cacheble plts

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.