Coder Social home page Coder Social logo

ratelimiter's Introduction

Description Build Status

ratelimiter is a simple c++ implementation of a thread safe, basic rate limiter.

Usage

Creating a Rate Limiter

There are multiple types of rate limiters, each with their own timing behaviour. However, they all implement the same interface. To create a rate limiter, simply:

RateLimiterInterface* limiter = new RateLimiter();
limiter->set_rate(1.0); // 1 Permit per second

You can of course choose another rate limiter (warm up, for example). Alternatively, if the specific rate limiter's consturctor supports it:

RateLimiterInterface* limiter = new RateLimiter(1.0); // Creates a ratelimiter with 1 permit per second

Using The Rate Limiter

There are two ways to aquire permits:

  • Blocking
  • Blocking with Timeout

For the basic blocking:

double wait_time = limiter->aquire(); // aquires 1 permit. Will block the thread until it is allowed to proceed
double wait_time = limiter->aquire(20); // aquires 20 permits. Will block the thread until it's allowed to proceed

Where wait_time is the amount of time that the thread was blocked for in milliseconds.

For the timeout blocking:

bool aquired = limiter->try_aquire(3000); // Same as try_aquire(1, 3000)
bool aquired = limiter->try_aquire(2, 3000);

On both cases, if try_aquire can aquire it's permits within the specified time (from now), it will block as long as necessary by the rate limiter, and then return true when it has aquired the permits. If it cannot aquire those permits within the specified time, then it will return IMMEDIATELY, with a value of false.

General Notes

These apply to all rate limiters in this library.

  • The first aquire happens instantly
  • Permits don't need to be returned
  • Changing the rate of a rate limiter will be applied on the NEXT aquire. For more information, see the test_rate() test.
  • Aquiring permits affect the next aquire, and not the current. That is, the next aquire will happen at the same time regardless of how many permits it requires. The amount of permits it aquired affects how long until the next aquire will be allowed.

ratelimiter's People

Watchers

James Cloos avatar Rajkumar Singh avatar

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.