Connexion is a flask application so many techniques that work with flask work with connexion.
We successfully used Flask-Limter to do rate limiting.
import os
import connexion
APP = connexion.FlaskApp(
__name__, specification_dir=os.environ.get("OPENAPI_LOCATION", ".")
)
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import service.global_app as global_app
# This is for rate limiting (flask-limiter)
# Ref: https://limits.readthedocs.io/en/stable/storage.html#storage-scheme
APP.app.config["RATELIMIT_STORAGE_URI"] = (
"redis://" f"{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB + 1}"
)
# Kill switch for rate limiter
APP.app.config["RATELIMIT_ENABLED"] = True
# Policy for what to do if REDIS is down
APP.app.config["RATELIMIT_IN_MEMORY_FALLBACK"] = True
APP.app.config["RATELIMIT_HEADERS_ENABLED"] = True
APP.app.config["RATELIMIT_SWALLOW_ERRORS"] = os.environ.get("ENV", "") != "DEV"
LIMITER = Limiter(
global_app.APP.app,
key_func=get_remote_address,
# 1
default_limits=[DAILY_LIMIT, HOURLY_LIMIT],
)
@LIMITER.limit("3/second", override_defaults=True, exempt_when=exempt_when)
def simple_search_dsl_async(
) -> str:
return "Hi"