api_server_config_guard.py 921 B

1234567891011121314151617181920212223
  1. """aiohttp middleware answering a config-backend outage instead of letting it end the gateway.
  2. ``ConfigBackendUnavailable`` is a ``SystemExit`` on purpose (config readers must not serve
  3. defaults instead of the selected backend's config). Escaping an aiohttp handler task, a
  4. ``SystemExit`` stops the event loop, so a remote-config plane blip during one request took the
  5. whole gateway down. This middleware turns it into that request's 503.
  6. """
  7. from __future__ import annotations
  8. from aiohttp import web
  9. from hermes_cli.config_backend import ConfigBackendUnavailable
  10. @web.middleware
  11. async def config_unavailable_middleware(request: web.Request, handler):
  12. try:
  13. return await handler(request)
  14. except ConfigBackendUnavailable as exc:
  15. return web.json_response(
  16. {"error": {"message": str(exc.code), "type": "server_error", "code": "config_backend_unavailable"}},
  17. status=503)