From bc8b331fe9dfb7a1b62702c635d22d7c6bd841bc Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 14 Oct 2022 12:49:28 -0400 Subject: [PATCH 1/2] Do not decrease flood violations --- app/app.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/app.py b/app/app.py index 1221152..e3ef13b 100644 --- a/app/app.py +++ b/app/app.py @@ -177,9 +177,6 @@ def create_app(args): if flood.is_banned(ip): abort(403, description="Too many request limits violations") - else: - if flood.has_violation(ip): - flood.decrease(ip) if args.api_keys: ak = get_req_api_key() From 70c457106743babe772616a38367c0336f4e198e Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 14 Oct 2022 13:27:34 -0400 Subject: [PATCH 2/2] Add --req-limit-storage redis support --- README.md | 1 + app/app.py | 1 + app/default_values.py | 5 +++++ app/main.py | 7 +++++++ requirements.txt | 1 + 5 files changed, 15 insertions(+) diff --git a/README.md b/README.md index f7be817..56dd5fa 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,7 @@ docker-compose -f docker-compose.cuda.yml up -d --build | --port | Set port to bind the server to | `5000` | LT_PORT | | --char-limit | Set character limit | `No limit` | LT_CHAR_LIMIT | | --req-limit | Set maximum number of requests per minute per client | `No limit` | LT_REQ_LIMIT | +| --req-limit-storage | Storage URI to use for request limit data storage. See [Flask Limiter](https://flask-limiter.readthedocs.io/en/stable/configuration.html) | `memory://` | LT_REQ_STORAGE | | --batch-limit | Set maximum number of texts to translate in a batch request | `No limit` | LT_BATCH_LIMIT | | --ga-id | Enable Google Analytics on the API client page by providing an ID | `No tracking` | LT_GA_ID | | --debug | Enable debug environment | `False` | LT_DEBUG | diff --git a/app/app.py b/app/app.py index e3ef13b..d58adbc 100644 --- a/app/app.py +++ b/app/app.py @@ -161,6 +161,7 @@ def create_app(args): default_limits=get_routes_limits( args.req_limit, args.daily_req_limit, api_keys_db ), + storage_uri=args.req_limit_storage, ) else: from .no_limiter import Limiter diff --git a/app/default_values.py b/app/default_values.py index 88a32ef..343f22c 100644 --- a/app/default_values.py +++ b/app/default_values.py @@ -56,6 +56,11 @@ _default_options_objects = [ 'default_value': -1, 'value_type': 'int' }, + { + 'name': 'REQ_LIMIT_STORAGE', + 'default_value': 'memory://', + 'value_type': 'str' + }, { 'name': 'DAILY_REQ_LIMIT', 'default_value': -1, diff --git a/app/main.py b/app/main.py index 0df79fb..6e24158 100644 --- a/app/main.py +++ b/app/main.py @@ -28,6 +28,13 @@ def get_args(): metavar="", help="Set the default maximum number of requests per minute per client (%(default)s)", ) + parser.add_argument( + "--req-limit-storage", + default=DEFARGS['REQ_LIMIT_STORAGE'], + type=str, + metavar="", + help="Storage URI to use for request limit data storage. See https://flask-limiter.readthedocs.io/en/stable/configuration.html. (%(default)s)", + ) parser.add_argument( "--daily-req-limit", default=DEFARGS['DAILY_REQ_LIMIT'], diff --git a/requirements.txt b/requirements.txt index b52b177..39a300f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ argos-translate-files==1.1.0 itsdangerous==2.1.2 Werkzeug==2.2.2 requests==2.28.1 +redis==4.3.4