Daily request limit

This commit is contained in:
Piero Toffanin 2021-05-16 10:57:19 -04:00
parent 800774f557
commit 4c5d828300
2 changed files with 11 additions and 4 deletions

View file

@ -22,7 +22,7 @@ def get_remote_address():
return ip 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: if default_req_limit == -1:
# TODO: better way? # TODO: better way?
default_req_limit = 9999999999999 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 "%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): def create_app(args):
from app.init import boot from app.init import boot
@ -73,12 +78,12 @@ def create_app(args):
if frontend_argos_language_target is None: if frontend_argos_language_target is None:
raise AttributeError(f"{args.frontend_language_target} as frontend target language is not supported.") 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 from flask_limiter import Limiter
limiter = Limiter( limiter = Limiter(
app, app,
key_func=get_remote_address, 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: else:
from .no_limiter import Limiter from .no_limiter import Limiter

View file

@ -12,6 +12,8 @@ def main():
help='Set character limit (%(default)s)') help='Set character limit (%(default)s)')
parser.add_argument('--req-limit', default=-1, type=int, metavar="<number>", parser.add_argument('--req-limit', default=-1, type=int, metavar="<number>",
help='Set the default maximum number of requests per minute per client (%(default)s)') 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="<number>",
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="<number of texts>", parser.add_argument('--batch-limit', default=-1, type=int, metavar="<number of texts>",
help='Set maximum number of texts to translate in a batch request (%(default)s)') help='Set maximum number of texts to translate in a batch request (%(default)s)')
parser.add_argument('--ga-id', type=str, default=None, metavar="<GA ID>", parser.add_argument('--ga-id', type=str, default=None, metavar="<GA ID>",