Skip to content

Latest commit

 

History

History
36 lines (31 loc) · 3.03 KB

File metadata and controls

36 lines (31 loc) · 3.03 KB

RateLimitModule.py

This module provides basic rate-limiting functionality for the AION RWX API. It uses an in-memory implementation, which is not suitable for production environments. This module is intended as a placeholder and should be replaced with a more robust and scalable solution for production deployments.

Code Explanation:

  • Configuration:
    • RATE_LIMIT_ENABLED: A boolean flag to enable or disable rate limiting.
    • RATE_WINDOW: The time window (in seconds) within which requests are counted.
    • RATE_MAX_REQUESTS: The maximum number of allowed requests within the defined time window.
  • REQUEST_HISTORY Dictionary:
    • This is a dictionary that stores the request history for each client (identified by IP address).
    • For each client IP, it maintains a list of timestamps representing the times of their requests.
    • Important: This in-memory storage is only suitable for development and testing. It will not persist across server restarts and is not scalable for multiple concurrent users.
  • setup_rate_limiting(app: FastAPI) Function:
    • This function sets up the rate-limiting middleware for the FastAPI application.
    • It defines an @app.middleware("http") that will be executed for every incoming HTTP request.
    • Inside the middleware:
      • It retrieves the client's IP address using request.client.host.
      • If RATE_LIMIT_ENABLED is True, it calls the check_rate_limit() function to enforce the rate limit.
      • It then proceeds to process the request using call_next(request).
  • check_rate_limit(client_ip: str) Function:
    • This function performs the actual rate limit check.
    • It retrieves the request history for the given client_ip from the REQUEST_HISTORY dictionary.
    • It removes any timestamps from the history that are older than the current time minus the RATE_WINDOW.
    • If the number of remaining requests in the history is greater than or equal to RATE_MAX_REQUESTS, it raises an HTTPException with a 429 Too Many Requests status code.
    • Finally, it adds the current timestamp to the request history for the client_ip.

Important Considerations:

  • Production Use: The in-memory rate limiting implemented in this module is not suitable for production. It is not persistent, scalable, or thread-safe.
  • Alternative Solutions: For production environments, consider using more robust and persistent rate-limiting solutions such as:
    • Redis: A popular in-memory data store that can be used for persistent and distributed rate limiting.
    • Leaky Bucket/Token Bucket Algorithms: Implement these algorithms using appropriate libraries or data structures.
    • FastAPI's SlowAPI: A FastAPI extension that provides rate limiting capabilities.
  • Client Identification: The current implementation identifies clients based on their IP address (request.client.host). This might not be accurate in all cases (e.g., behind proxies). Consider using more sophisticated client identification methods if necessary.