
    `gj~                    P    d Z ddlmZ ddlZ	 ddlZdZdZddZy# e$ r dZY w xY w)u(  Shared HTTP client factory for long-lived platform adapters.

Gateway messaging platforms (QQ Bot, Feishu, WeCom, DingTalk, Signal,
BlueBubbles, WeCom-callback) keep a persistent ``httpx.AsyncClient``
alive for the adapter's lifetime.  That amortises TLS/connection setup
across many API calls, but it also means the process's file-descriptor
pressure is sensitive to how aggressively the pool recycles idle keep-
alive connections.

httpx's default ``keepalive_expiry`` is 5 seconds.  On macOS behind
Cloudflare Warp (and other transparent proxies), peer-initiated FIN can
sit in ``CLOSE_WAIT`` longer than that before the local socket actually
drains — which, multiplied across 7 long-lived adapters plus the LLM
client and MCP clients, walks straight into the default 256 fd limit.
See #18451.

``platform_httpx_limits()`` returns a tighter ``httpx.Limits`` the
adapter factories use instead of the httpx default.  The values chosen:

* ``max_keepalive_connections=10`` — plenty for any single adapter;
  platform APIs rarely parallelise beyond this.
* ``keepalive_expiry=2.0`` — close idle sockets aggressively so a
  proxy's lingering CLOSE_WAIT window can't starve the process.

Override via ``HERMES_GATEWAY_HTTPX_KEEPALIVE_EXPIRY`` /
``HERMES_GATEWAY_HTTPX_MAX_KEEPALIVE`` env vars when tuning under load.
    )annotationsNg       @
   c                     t         ydd} dd} | dt              } |dt              }t        j                  ||      S )	zReturn ``httpx.Limits`` tuned for persistent platform-adapter clients.

    Returns ``None`` when httpx isn't importable, so callers can fall
    back to httpx's built-in default without a hard dependency on this
    helper being reachable.
    Nc                    t         j                  j                  | d      j                         }|s|S 	 t	        |      }|dkD  r|S |S # t
        t        f$ r |cY S w xY wN r   )osenvirongetstripfloat	TypeError
ValueErrornamedefaultrawvals       X/root/.hermes/venv/lib/python3.12/site-packages/gateway/platforms/_http_client_limits.py
_env_floatz)platform_httpx_limits.<locals>._env_float5   sc    jjnnT2&,,.N	*C Ags*7* :& 	N	   A AAc                    t         j                  j                  | d      j                         }|s|S 	 t	        |      }|dkD  r|S |S # t
        t        f$ r |cY S w xY wr   )r	   r
   r   r   intr   r   r   s       r   _env_intz'platform_httpx_limits.<locals>._env_int?   sc    jjnnT2&,,.N	c(C Ags*7* :& 	N	r   %HERMES_GATEWAY_HTTPX_KEEPALIVE_EXPIRY"HERMES_GATEWAY_HTTPX_MAX_KEEPALIVE)max_keepalive_connectionskeepalive_expiry)r   strr   r   returnr   )r   r   r   r   r    r   )httpx_DEFAULT_KEEPALIVE_EXPIRY_S_DEFAULT_MAX_KEEPALIVELimits)r   r   r   max_keepalives       r   platform_httpx_limitsr&   +   sT     }++ "/1L ,.DM <<"/)     )r    z'httpx.Limits | None')	__doc__
__future__r   r	   r!   ImportErrorr"   r#   r&    r'   r   <module>r,      sC   8 # 	
 "  )  Es    %%