From 4c5d828300dc693e90e5bc89c21d0c1b063afad8 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sun, 16 May 2021 10:57:19 -0400 Subject: [PATCH] Daily request limit --- app/app.py | 13 +++++++++---- app/main.py | 2 ++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/app.py b/app/app.py index 2b0466e..d0723e1 100644 --- a/app/app.py +++ b/app/app.py @@ -22,7 +22,7 @@ def get_remote_address(): return ip -def get_routes_limits(default_req_limit, api_keys_db): +def get_routes_limits(default_req_limit, daily_req_limit, api_keys_db): if default_req_limit == -1: # TODO: better way? default_req_limit = 9999999999999 @@ -44,7 +44,12 @@ def get_routes_limits(default_req_limit, api_keys_db): return "%s per minute" % req_limit - return [limits] + res = [limits] + + if daily_req_limit > 0: + res.append("%s per day" % daily_req_limit) + + return res def create_app(args): from app.init import boot @@ -73,12 +78,12 @@ def create_app(args): if frontend_argos_language_target is None: raise AttributeError(f"{args.frontend_language_target} as frontend target language is not supported.") - if args.req_limit > 0 or args.api_keys: + if args.req_limit > 0 or args.api_keys or args.daily_req_limit > 0: from flask_limiter import Limiter limiter = Limiter( app, key_func=get_remote_address, - default_limits=get_routes_limits(args.req_limit, Database() if args.api_keys else None) + default_limits=get_routes_limits(args.req_limit, args.daily_req_limit, Database() if args.api_keys else None) ) else: from .no_limiter import Limiter diff --git a/app/main.py b/app/main.py index 21aadcd..55a7d62 100644 --- a/app/main.py +++ b/app/main.py @@ -12,6 +12,8 @@ def main(): help='Set character limit (%(default)s)') parser.add_argument('--req-limit', default=-1, type=int, metavar="", help='Set the default maximum number of requests per minute per client (%(default)s)') + parser.add_argument('--daily-req-limit', default=-1, type=int, metavar="", + help='Set the default maximum number of requests per day per client, in addition to req-limit. (%(default)s)') parser.add_argument('--batch-limit', default=-1, type=int, metavar="", help='Set maximum number of texts to translate in a batch request (%(default)s)') parser.add_argument('--ga-id', type=str, default=None, metavar="",