Skip to content

Commit

Permalink
Tacs 50 implement rate limiter (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasanchez authored May 14, 2023
2 parents 0149af8 + 92c23e7 commit 1508fe5
Show file tree
Hide file tree
Showing 18 changed files with 1,005 additions and 16 deletions.
22 changes: 21 additions & 1 deletion api-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,24 @@ This project uses the `Docker` image `uvicorn-gunicorn-fastapi:python3.10-slim`
- [FastAPI](https://fastapi.tiangolo.com/)
- [Pydantic](https://pydantic-docs.helpmanual.io/)
- [Cosmic Python](https://cosmicpython.com/)
- [Gunicorn + Uvicorn](https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker)
- [Gunicorn + Uvicorn](https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker)

## Design

### Rate Limiting

This API Gateway uses a `Redis` instance to store the rate limit counters.
The rate limit is set to `X` requests per `T` seconds.

#### Class Diagram

Our `FastAPI` application calls a middleware function which checks the rate limit before request is served.

It uses the `RateLimiter` to increment the counter for the request host and checks if it has exceeded the limit, if so,
it returns it interrupts the execution sending a response with a `429` status code: too many requests.

![Rate Limiting Class Diagram](../docs/assets/rate-limiter-class_diagram.svg)

The `RateLimiter` implementation uses the `Redis` instance to store the counters with the `INCR` command, which
guarantees that is run atomically. `RedisConnector` is a wrapper around the `Redis` client which provides common methods
an errors for using both a single redis connection or a cluster. `RedisClient` uses `aioredis` for as ync operations.
Loading

0 comments on commit 1508fe5

Please sign in to comment.