kaizen-ai / kaizenflow

KaizenFlow is a framework for Bayesian reasoning and AI/ML stream computing
GNU General Public License v3.0
112 stars 76 forks source link

Spring2024_Rate_Limiting_API_with_Redis #775

Closed Bodarnn closed 4 months ago

Bodarnn commented 7 months ago

Description: Develop an API service utilizing Flask or FastAPI to enforce rate limiting, restricting the frequency of requests per user within a defined timeframe. Utilize Redis for efficient request tracking, managing request counters, and expiration times. Implement comprehensive rate-limiting logic to ensure fair resource allocation and prevent abuse. Enhance the project by exploring additional features such as token-based authentication, dynamic rate limiting based on user roles, or distributed rate limiting across multiple instances. Properly handle responses when users exceed the rate limit, providing informative error messages or status codes.

Link: https://docs.google.com/document/d/1GEOmfpBUXiCua18wR1Hx1OMUVlku-1of/edit#heading=h.l5uzn24bcoy4

Bodarnn commented 6 months ago

SOLVED

My app works in Python, but does not seem to work in Docker.

Here is Python:

> python3 app.py
 * Serving Flask app 'app'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
image

And here is Docker:

$ docker compose up
[+] Running 1/0
 ✔ Container app-web-1  Created                                            0.0s 
Attaching to web-1
web-1  |  * Serving Flask app 'app'
web-1  |  * Debug mode: off
web-1  | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web-1  |  * Running on http://127.0.0.1:5000
web-1  | Press CTRL+C to quit
image
$ docker ps
CONTAINER ID   IMAGE     COMMAND           CREATED         STATUS          PORTS                                       NAMES
4dd16ee8c39b   app-web   "python app.py"   4 minutes ago   Up 14 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   app-web-1

The container looks good, but I can't seem to connect.

SOLUTION

The app was only listening on 127.0.0.1. I changed the arguments of the run function:

app.run(host='0.0.0.0', port=5000)

I also cleaned my Docker docker system prune --all --force --volumes.

After that, everything looks good:

$ docker compose up
[+] Running 1/0
 ✔ Container app-web-1  Created                                            0.0s 
Attaching to web-1
web-1  |  * Serving Flask app 'app'
web-1  |  * Debug mode: off
web-1  | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web-1  |  * Running on all addresses (0.0.0.0)
web-1  |  * Running on http://127.0.0.1:5000
web-1  |  * Running on http://172.22.0.2:5000
web-1  | Press CTRL+C to quit
image