A rate limiter is a software or hardware component used to control the rate at which certain operations or requests are allowed to occur within a system or application. It is a crucial tool for managing and regulating the flow of data, requests, or actions to prevent overuse of resources, maintain system stability, and protect against abuse or denial-of-service attacks.
Table of Contents
- NodeJS
- TypeScript
- ExpressJS
- Jest
This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.
This is an example of how to list things you need to use the software and how to install them.
- npm
npm install npm@latest -g
- Clone the repo
git clone https://github.com/tanmayidev/rate-limiter.git
- Install NPM packages
npm install
- Run the project
npm run start
Use this space to show useful examples of how a project can be used. Additional screenshots, code examples and demos work well in this space. You may also link to more resources.
-
Endpoints:
This service provides two endpoints:
/limited: This endpoint is rate-limited./unlimited: This endpoint has no rate limits.
-
Custom Rate Limiting:
To specify a custom rate limit and rate-limiting algorithm for a request, use the following HTTP headers:
-
origin-ipaddress(Mandatory for/limitedendpoint): Set this header to specify the custom IP address for rate limiting. -
rate-limiting-algo: Use this header to specify the rate limiting algorithm. It accepts the following values:0: Token Bucket 1: Fixed Window Counter 2: Sliding Window Log 3: Sliding Window Counter
-
-
Default Algorithm:
If the
rate-limiting-algoheader is not specified, it defaults to the Token Bucket algorithm.
Postman configurations to stress-test the apis
The following script randomly picks ip-addresses from the given list (ipAddrs). Please see Dynamic-variables and Using-variables.
function pickIpAddr() {
const ipAddrs = ['143.33.72.232', '42.33.106.86', '164.78.11.195', '216.217.171.55', '181.81.57.111'];
const idx = _.random(0, 4);
return ipAddrs[idx];
}
pm.variables.set('custom_ip', pickIpAddr());- The
origin-ipaddressis assigned a dynamic-variable ({{custom_ip}}), which is evaluated in the Pre-Request Script. - The
rate-limiting-algohttp-header specifies appropriate algorithm to be applied. Look at Enum, Step-2 in usage
origin-ipaddress: {{custom_ip}}
rate-limiting-algo: 0
- Token Bucket
- Fixed Window Counter
- Sliding Window Log
- Sliding Window Counter
- Implement the algorithm
- Integrate with Load-Balancer in a horizontally scaled architecture.
- Write unit-tests
See the open issues for a full list of proposed features (and known issues).
- Token Bucket Performance Report
- Fixed Windows Counter Performance Report
- Sliding Windows Log Performance Report
- Sliding Window Counter Performance Report
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the ISC License. MIT License