An open API service indexing awesome lists of open source software.

https://github.com/cafetechne/flask_limiter_firestore

A Firestore backend for Flask-Limiter that enables serverless, distributed rate limiting without Redis or Memcached.
https://github.com/cafetechne/flask_limiter_firestore

backend firestore flask rate-limiter rate-limiting

Last synced: about 1 month ago
JSON representation

A Firestore backend for Flask-Limiter that enables serverless, distributed rate limiting without Redis or Memcached.

Awesome Lists containing this project

README

          

๐Ÿ”ง Features
-----------
- โœ… Drop-in replacement for Redis/Memcached backends
- โ˜๏ธ Firestore-compatible (GCP-managed, serverless, global scale)
- ๐Ÿงน Built-in TTL auto-cleanup via `expires_at` field
- ๐Ÿ” No extra infrastructure needed on Google App Engine/Cloud Run
- ๐Ÿงช Fully compatible with Flask-Limiter โ‰ฅ3.5+

๐Ÿ“ฆ Installation
---------------
pip install Flask-Limiter-Firestore

๐Ÿš€ Usage
--------
from flask import Flask, request
from flask_limiter import Limiter
from flask_limiter_firestore import FirestoreStorage

def get_client_ip():
xff = request.headers.get("X-Forwarded-For", "")
return xff.split(",")[0].strip() if xff else request.remote_addr

app = Flask(__name__)
limiter = Limiter(
app=app,
key_func=get_client_ip,
storage=FirestoreStorage(collection_name="rate_limits"),
default_limits=["10 per minute"],
)

๐Ÿ” Authentication
-----------------
If you're running locally, authenticate with:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json

On GAE / Cloud Run / Cloud Functions: ADC is automatic.

โœ… Example Route
----------------
@app.route("/api/data")
@limiter.limit("5 per minute")
def data():
return "Rate limited!"

๐Ÿงน Cleanup Policy
-----------------
Use a TTL index on the `expires_at` field in Firestore for auto-deletion.

๐Ÿ› Troubleshooting
------------------
- FirestoreStorage.incr() got unexpected keyword 'amount' โ†’ Upgrade Flask-Limiter โ‰ฅ 3.5
- Invalid document key โ†’ Avoid slashes in limiter keys
- 'wrap_exceptions' attribute missing โ†’ ensure base_exceptions property exists

๐Ÿ”— Links
--------
- Flask-Limiter: https://flask-limiter.readthedocs.io
- Firestore: https://cloud.google.com/firestore
- PyPI: https://pypi.org/project/Flask-Limiter-Firestore

๐Ÿ“„ License
----------
MIT License