Coder Social home page Coder Social logo

rsghotra / rate-limiter-cpp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 4sily/rate-limiter-cpp

0.0 0.0 0.0 207 KB

A quick-and-dirty implementation of RPS limiter. Self-contained, no dependencies on any third-parties. Should not depend on platform, however, was tested only with Visual Studio.

License: MIT License

C 1.37% C++ 95.73% CMake 2.90%

rate-limiter-cpp's Introduction

License: MIT

Simple rate limiter

This repository contains a C++ implementation of a very primitive rate limiter.

Dependencies

Only Standard C++ library. No boost or any third parties. The code does not depend on any platform specifics, but was developed and tested only with MSVC on Windows. The build outcome is a console application that will run the tests for the limiter. Simply clone, compile and run. C++14 compiler is required (e.g. Visual Studio >= 2015).

Algorithm

The basic idea takes into account the following thoughts:

  • To decide if the incoming request should be accepted/rejected we need to somehow keep the actual sum of hits/requests, which implies:
    • incrementing it as the valid request comes
    • decrementing it as the clock ticks
  • This may lead to the incorrect solution, when we simply calculate the sum per second.
  • But, what if we just keep sum counts per finer time intervals?

Hence, here's the plan.

  • Let's say we split 1-second interval into N time frames.
  • For each of them we will store the number of requests within that time frame in the corresponding element of the circular buffer.
  • As the time goes, we will be moving our "sliding window" in that buffer, so for each step
    • we append one time frame with count = 0 to the right
    • we throw away the left-most element of the buffer
  • Every such step happens per timer tick, which is emitted every 1/N seconds.

Here's the illustration.

Alt

Implementation

Alt

What can be improved here

TL;DR: A lot. This is merely a "proof-of-concept" implementation made in a "quick-and-dirty" manner.

Some specific things to do:

  • Turn this console application into the static/dynamic library
  • Move tests to the separate library/application
  • Use some of the existing testing frameworks in that library/applicaton
    • e.g. Catch/Googletest/Boost.Test/doctest/whatever
  • (Arguable) use Boost.Circular buffer instead of the self-implemented inside the class HitQueue
  • Improve timer implemenation; preferrably use some 3rd party
  • Abstract away the interface of Limiter via either abstract base class or template trait class
    • Also de-couple the tests from the specific impelemntation of Limiter
  • Add several other implementations, especially for the standard rate limiting algorithms

rate-limiter-cpp's People

Contributors

4sily avatar vukis 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.