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.
- Host: GitHub
- URL: https://github.com/cafetechne/flask_limiter_firestore
- Owner: cafeTechne
- Created: 2025-05-05T02:58:41.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-13T20:03:13.000Z (about 1 year ago)
- Last Synced: 2025-09-15T18:53:52.321Z (9 months ago)
- Topics: backend, firestore, flask, rate-limiter, rate-limiting
- Language: Python
- Homepage: https://pypi.org/project/flask-limiter-firestore/0.0.0/
- Size: 15.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
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