Merge pull request #330 from LibreTranslate/redis

Support for persistent request limit storage via redis
This commit is contained in:
Piero Toffanin 2022-10-14 13:35:02 -04:00 committed by GitHub
commit 69762ce4f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 3 deletions

View file

@ -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 | | --port | Set port to bind the server to | `5000` | LT_PORT |
| --char-limit | Set character limit | `No limit` | LT_CHAR_LIMIT | | --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 | 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 | | --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 | | --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 | | --debug | Enable debug environment | `False` | LT_DEBUG |

View file

@ -161,6 +161,7 @@ def create_app(args):
default_limits=get_routes_limits( default_limits=get_routes_limits(
args.req_limit, args.daily_req_limit, api_keys_db args.req_limit, args.daily_req_limit, api_keys_db
), ),
storage_uri=args.req_limit_storage,
) )
else: else:
from .no_limiter import Limiter from .no_limiter import Limiter
@ -177,9 +178,6 @@ def create_app(args):
if flood.is_banned(ip): if flood.is_banned(ip):
abort(403, description="Too many request limits violations") abort(403, description="Too many request limits violations")
else:
if flood.has_violation(ip):
flood.decrease(ip)
if args.api_keys: if args.api_keys:
ak = get_req_api_key() ak = get_req_api_key()

View file

@ -56,6 +56,11 @@ _default_options_objects = [
'default_value': -1, 'default_value': -1,
'value_type': 'int' 'value_type': 'int'
}, },
{
'name': 'REQ_LIMIT_STORAGE',
'default_value': 'memory://',
'value_type': 'str'
},
{ {
'name': 'DAILY_REQ_LIMIT', 'name': 'DAILY_REQ_LIMIT',
'default_value': -1, 'default_value': -1,

View file

@ -28,6 +28,13 @@ def get_args():
metavar="<number>", 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(
"--req-limit-storage",
default=DEFARGS['REQ_LIMIT_STORAGE'],
type=str,
metavar="<Storage URI>",
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( parser.add_argument(
"--daily-req-limit", "--daily-req-limit",
default=DEFARGS['DAILY_REQ_LIMIT'], default=DEFARGS['DAILY_REQ_LIMIT'],

View file

@ -16,3 +16,4 @@ argos-translate-files==1.1.0
itsdangerous==2.1.2 itsdangerous==2.1.2
Werkzeug==2.2.2 Werkzeug==2.2.2
requests==2.28.1 requests==2.28.1
redis==4.3.4