1. Learn
  2. /
  3. Courses
  4. /
  5. Deploying AI into Production with FastAPI

Connected

Exercise

Implementing rate limiter

You're building a sentiment analysis API where users can analyze texts for sentiments. To prevent abuse, you need to implement rate limiting that allows only 5 requests per minute per API key. The RateLimiter class is already created and you have to add the is_rate_limited method within the RateLimiter class that checks the number of requests that have been made within the 1 minute time window.

The datetime and timedelta classes from the datetime library have been pre-imported.

Instructions

100 XP
  • Get the current time and calculate the timestamp for one minute ago.
  • Filter the request list to keep only timestamps within the last minute.
  • Check if the number of recent requests exceeds the limit.