Giter VIP home page Giter VIP logo

limitless's Introduction

limitless

Build Status

Limitless is a lightweight, generic library for Erlang to quickly integrate a rate-limiter inside your application.

Example

See examples directory to see how use the library with the mongodb backend.

Configuration

Add limitless to your rebar.config deps:

{deps,
 [
  {limitless, "",
   {git, "https://github.com/rpt/limitless.git",
    {tag, "v1.1.0"}}}
 ]}.

Remember to add also the libraries needed by the specific backend (in the mongodb example: uuid, mongopool).

Add a configuration for specify which backend are you using and which kind of limits are you imposing.

In this example we define that every request of type token will be controlled by two different limits:

  1. The first limit is 1000 req/day
  2. The other limit is 100 req/15min
[
  {limitless, [
    {backend, [
      % specify the backend to use
      {name, limitless_backend_mongopool},
      % and the backend configuration
      {config, [
        {table, limitless},
        {pool, mymongopool}
      ]}
    ]},
    {limits, [
      {token, [
        [
          % max 1000 req/day
          {type, <<"Token-Daily">>},
          {frequency, 86400}, % 1 day = 3600 * 24h
          {requests, 1000}
        ],
        [
          % max 100 req/15min
          {type, <<"Token-15min">>},
          {frequency, 900}, % 15 min = 60 * 15
          {requests, 100}
        ]
      ]}
    ]}
  ]}
]

Note: in the configuration there is also a term mongopool to complete the backend configuration.

On the application running, when a new object token we'll be created, also should be registered in the limits lists with:

{ok, Ctx} = limitless:init().
limitless:setup(RequestToken, token, ctx).

In this way, every time you receive a request from a token RequestToken, you be able to check if it reached any of its limits (in the example: 1000 req/day and 100 req/15min):

{Result, ConsumedTokens, InfoTokens} = limitless:is_reached(
                                                [RequestToken], LimitlessCtx)

If Result is true, it means that at least one limit is reached (see ConsumedTokens to know which one).

The InfoTokens are useful information to build a table about the current situation.

In the example, it's used to build the HTTP relative headers:

Req2 = limitless_cowboy_utils:set_rate_limiter_headers(InfoTokens, Req),

It'll add the following headers to the cowboy response:

X-RateLimit-Token-Daily-Limit: 1000
X-RateLimit-Token-Daily-Remaining: 1000
X-RateLimit-Token-Daily-Reset: 86250
X-RateLimit-Token-15min-Limit: 100
X-RateLimit-Token-15min-Remaining: 100
X-RateLimit-Token-15min-Reset: 750

Status

Stable release.

limitless's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

m-2k laymer

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.