Skip to content

This Python project implements a Rate Limiter decorator that restricts the number of function calls within a specified time window. It helps control execution flow and prevent excessive function calls.

License

Notifications You must be signed in to change notification settings

ParisaAlizadeh2003/SafeRateLimiter-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SafeRateLimiter

A Python project that implements a Rate Limiter decorator to restrict function calls within a specified time window. This helps prevent excessive calls and protects system resources.


Description

RateLimiterPy uses Python decorators to monitor and control how many times a function can be called within a given time frame. By storing timestamps of recent calls in a local list (closure), the decorator allows a function to run only if the number of calls in the defined period is below a specified limit. If the limit is exceeded, a ValueError is raised.


Features

  • Function Call Control: Limit the number of function executions per time period.
  • High-Resolution Timing: Uses time.perf_counter() for precise measurement.
  • Closure-Based Storage: Keeps call timestamps local to each decorated function.
  • Simple and Extensible: Easy to apply to any function requiring rate limiting.

Installation

  1. Clone the repository:

    git clone https://github.com/YourUsername/RateLimiterPy.git
    cd RateLimiterPy
  2. (Optional) Create a virtual environment and install dependencies:

    python -m venv env
    source env/bin/activate  # On Windows use: env\Scripts\activate
    pip install -r requirements.txt

    Note: This project uses only standard libraries (e.g., time, functools), so no additional packages are required.


Usage

Apply the rate limiter decorator to your function by specifying the maximum number of allowed calls and the time window (in seconds).

Example:

import time
from Rate_Limiter import Rate_limiter

@Rate_limiter(3, 5)
def say_hello(name):
    return f"Hello to {name}"

if __name__ == "__main__":
    # This loop will raise a ValueError once the rate limit is exceeded
    for _ in range(10):
        try:
            print(say_hello("sara"))
        except ValueError as e:
            print(e)
    
    time.sleep(5)  # Wait for the rate limiter to reset
    print(say_hello("ali"))

Testing

Tests are written using pytest to verify the behavior under various conditions including valid calls, invalid (exceeding) calls, and edge cases.

Run Tests:

  1. Ensure you have pytest installed:

    pip install pytest
  2. Run the tests:

    pytest

Example Test Cases:

  • Valid Calls: Confirm that functions return correct values when calls are within limits.
  • Invalid Calls: Verify that a ValueError is raised when the rate limit is exceeded.
  • Edge Cases: Test boundary conditions with appropriate use of time.sleep() to reset the limiter.

Contributing

Contributions are welcome! If you have suggestions or improvements:

  • Fork the repository.
  • Create a feature branch.
  • Commit your changes and open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Contact

For any questions or issues, please open an issue on GitHub or contact Parisa Alizadeh.


About

This Python project implements a Rate Limiter decorator that restricts the number of function calls within a specified time window. It helps control execution flow and prevent excessive function calls.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages