_http_client_limits.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """Shared HTTP client factory for long-lived platform adapters.
  2. httpx's default ``keepalive_expiry`` (5s) lets peer-initiated FIN sit in
  3. ``CLOSE_WAIT`` behind transparent proxies (macOS + Cloudflare Warp); across 7
  4. adapters plus LLM/MCP clients that walks into the default 256 fd limit.
  5. ``platform_httpx_limits()`` returns tighter ``httpx.Limits``: 10 keepalive
  6. connections (platform APIs rarely parallelise beyond this), 2.0s expiry.
  7. Override via ``HERMES_GATEWAY_HTTPX_KEEPALIVE_EXPIRY`` / ``HERMES_GATEWAY_HTTPX_MAX_KEEPALIVE``.
  8. """
  9. from __future__ import annotations
  10. import os
  11. try:
  12. import httpx
  13. except ImportError: # pragma: no cover — optional dep
  14. httpx = None # type: ignore[assignment]
  15. _DEFAULT_KEEPALIVE_EXPIRY_S = 2.0
  16. _DEFAULT_MAX_KEEPALIVE = 10
  17. def _positive_env(name: str, default, cast):
  18. """``cast(env)`` when set, parseable and > 0; else *default*."""
  19. raw = os.environ.get(name, "").strip()
  20. if not raw:
  21. return default
  22. try:
  23. val = cast(raw)
  24. except (TypeError, ValueError):
  25. return default
  26. return val if val > 0 else default
  27. def platform_httpx_limits() -> httpx.Limits | None:
  28. """``httpx.Limits`` tuned for persistent platform-adapter clients; ``None`` without httpx."""
  29. if httpx is None:
  30. return None
  31. # max_connections stays at the httpx default (100) — plenty of headroom.
  32. return httpx.Limits(
  33. max_keepalive_connections=_positive_env(
  34. "HERMES_GATEWAY_HTTPX_MAX_KEEPALIVE", _DEFAULT_MAX_KEEPALIVE, int),
  35. keepalive_expiry=_positive_env(
  36. "HERMES_GATEWAY_HTTPX_KEEPALIVE_EXPIRY", _DEFAULT_KEEPALIVE_EXPIRY_S, float),
  37. )