Merge pull request #584 from pierotofy/timecost

Add --req-time-cost
This commit is contained in:
Piero Toffanin 2024-02-05 15:22:24 -05:00 committed by GitHub
commit e34687df9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 52 additions and 26 deletions

View file

@ -9,8 +9,6 @@ repos:
name: " ✔️ Check TOML" name: " ✔️ Check TOML"
- id: check-json - id: check-json
name: " ✔️ Check JSON" name: " ✔️ Check JSON"
- id: trailing-whitespace
name: " ✂️ Trim trailing whitespaces"
- repo: https://github.com/charliermarsh/ruff-pre-commit - repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.277 rev: v0.0.277
hooks: hooks:

View file

@ -166,12 +166,13 @@ Arguments passed to the process or set via environment variables are split into
### Configuration Parameters ### Configuration Parameters
| Argument | Description | Default Parameter | Env. name | | Argument | Description | Default Parameter | Env. name |
|-----------------------------|-------------------------------------------------------------------------------------------------------------| -------------------- |------------------------------| | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- | --------------------------- |
| --host | Set host to bind the server to | `127.0.0.1` | LT_HOST | | --host | Set host to bind the server to | `127.0.0.1` | LT_HOST |
| --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 (outside of limits set by api keys) | `No limit` | LT_REQ_LIMIT | | --req-limit | Set maximum number of requests per minute per client (outside of limits set by api keys) | `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_LIMIT_STORAGE | | --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_LIMIT_STORAGE |
| --req-time-cost | Considers a time cost (in seconds) for request limiting purposes. If a request takes 10 seconds and this value is set to 5, the request cost is either 2 or the actual request cost (whichever is greater). | `No time cost` | LT_REQ_TIME_COST |
| --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 | `Empty (no tracking)` | LT_GA_ID | | --ga-id | Enable Google Analytics on the API client page by providing an ID | `Empty (no tracking)` | LT_GA_ID |
| --frontend-language-source | Set frontend default language - source | `auto` | LT_FRONTEND_LANGUAGE_SOURCE | | --frontend-language-source | Set frontend default language - source | `auto` | LT_FRONTEND_LANGUAGE_SOURCE |

View file

@ -1,4 +1,5 @@
import io import io
import math
import os import os
import re import re
import tempfile import tempfile
@ -219,6 +220,13 @@ def create_app(args):
from flask_limiter import Limiter from flask_limiter import Limiter
def limits_cost():
req_cost = getattr(request, 'req_cost', 1)
if args.req_time_cost > 0:
return max(req_cost, int(math.ceil(getattr(request, 'duration', 0) / args.req_time_cost)))
else:
return req_cost
limiter = Limiter( limiter = Limiter(
key_func=get_remote_address, key_func=get_remote_address,
default_limits=get_routes_limits( default_limits=get_routes_limits(
@ -226,7 +234,7 @@ def create_app(args):
), ),
storage_uri=args.req_limit_storage, storage_uri=args.req_limit_storage,
default_limits_deduct_when=lambda req: True, # Force cost to be called after the request default_limits_deduct_when=lambda req: True, # Force cost to be called after the request
default_limits_cost=lambda: getattr(request, 'req_cost', 1) default_limits_cost=limits_cost
) )
else: else:
from .no_limiter import Limiter from .no_limiter import Limiter
@ -325,12 +333,19 @@ def create_app(args):
status = e.code status = e.code
raise e raise e
finally: finally:
duration = max(default_timer() - start_t, 0) request.duration = max(default_timer() - start_t, 0)
measure_request.labels(request.path, status, ip, ak).observe(duration) measure_request.labels(request.path, status, ip, ak).observe(request.duration)
g.dec() g.dec()
return measure_func return measure_func
else: else:
return func @wraps(func)
def time_func(*a, **kw):
start_t = default_timer()
try:
return func(*a, **kw)
finally:
request.duration = max(default_timer() - start_t, 0)
return time_func
@bp.errorhandler(400) @bp.errorhandler(400)
def invalid_api(e): def invalid_api(e):

View file

@ -81,6 +81,11 @@ _default_options_objects = [
'default_value': -1, 'default_value': -1,
'value_type': 'int' 'value_type': 'int'
}, },
{
'name': 'REQ_TIME_COST',
'default_value': -1,
'value_type': 'int'
},
{ {
'name': 'BATCH_LIMIT', 'name': 'BATCH_LIMIT',
'default_value': -1, 'default_value': -1,

View file

@ -63,6 +63,13 @@ def get_args():
metavar="<number>", metavar="<number>",
help="Set the maximum number of request limit offences that a client can exceed before being banned. (%(default)s)", help="Set the maximum number of request limit offences that a client can exceed before being banned. (%(default)s)",
) )
parser.add_argument(
"--req-time-cost",
default=DEFARGS['REQ_TIME_COST'],
type=int,
metavar="<number>",
help="Considers a time cost (in seconds) for request limiting purposes. If a request takes 10 seconds and this value is set to 5, the request cost is either 2 or the actual request cost (whichever is greater). (%(default)s)",
)
parser.add_argument( parser.add_argument(
"--batch-limit", "--batch-limit",
default=DEFARGS['BATCH_LIMIT'], default=DEFARGS['BATCH_LIMIT'],