token bucket implemented in memory, in app

This commit is contained in:
2023-08-10 12:28:09 +03:00
commit 224cc4f5ec
5 changed files with 59 additions and 0 deletions

22
my_limiter/wsgi.py Normal file
View File

@@ -0,0 +1,22 @@
import flask as f
from . import algos
application = f.Flask(__name__)
increment_requests_func = algos.token_bucket
@application.before_request
def before_request():
ip = f.request.remote_addr
try:
increment_requests_func(ip)
except algos.TooManyRequests:
return f.abort(429)
@application.route('/')
def home():
return '<!doctype html><title>Hello</title><h1>Hello</h1>'