Implementing Rate Limiting in a Python 3.x REST API for Mobile App
I'm prototyping a solution and I'm updating my dependencies and I'm testing a new approach and Hey everyone, I'm running into an issue that's driving me crazy... I've been banging my head against this for hours. While developing a RESTful API with Flask for a mobile application, I need to implement rate limiting to prevent abuse and ensure fair usage of resources. The API will serve multiple clients, and I want to restrict each client to, let's say, 100 requests per hour. I came across Flask-Limiter, which seems promising, but Iโm unsure how to configure it appropriately. Iโve tried a few basic setups. For instance, I initialized it as follows: ```python from flask import Flask from flask_limiter import Limiter app = Flask(__name__) limiter = Limiter(app, key_func=get_remote_address) ``` However, when I decorate my route: ```python @limiter.limit("100/hour") @app.route('/api/data') def get_data(): return {'data': 'some data'} ``` It doesnโt seem to enforce the limit correctly. I tested with a REST client by sending requests in quick succession, and Iโm not receiving any 429 (Too Many Requests) errors. I also noticed that the key function Iโm using is only returning the remote address. Is there a better way to identify users, perhaps using API keys or tokens? Additionally, I read about different storage backends for rate limiting, and Iโm curious if using Redis would enhance performance in a production environment. How can I configure this with Flask-Limiter? Any insights on best practices for setting this up would be greatly appreciated! I'm working on a service that needs to handle this. Any help would be greatly appreciated! I'm working with Python in a Docker container on macOS. Has anyone else encountered this? I'm developing on Ubuntu 20.04 with Python. Thanks for taking the time to read this!