This is an pythonic example of TokenBucket algorithm to implement rate limiter.
A rate limiter is a crucial component in software application when the resources are exposed to outside world and the stakeholders want to control how the resources are being manipulated.
A rate limiter can be implemented to prevent any DoS or DDoS attack from malicious parties. Dos attack is continious demand of computing resources making evenly resouce distribution impossible and room for resource starvation.
A rate limiter is also often implemented when the stakeholders want to limit the number of requests permitted to evenly distribute the computing resources to all the parties.
The bucket size i.e. the total tokens in the bucket.
token_size
Frequency after how many minutes the bucket will be auto reset.
refill_rate_time_in_minutes
How many seconds to wait for the next request to the api.
seconds_to_wait_to_simulate_request
The main interface to the resource - a calculator
api.py
The logic to limit the request frequency to the api to access the calculator
rate_limiter.py
The resource to share through the api - functionalities of a calculator
calculator.py
Clone the project
https://github.com/Mahboob-A/rate-limiter.git
Go to the project directory
cd rate_limiter
Start the server
- for Linux/MacOS
python3 api.py
- for windows
python api.py
The tokenbucket algorithm implemented here is slightly different than the standard how it is done.
I have customized it while analyzing the requirements.
TokenBucket Algorithm
Python