diff --git a/README.md b/README.md index 3948086..fbcd2f0 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,10 @@ All settings are configured through environment variables prefixed with `TERMINA | `TERMINALS_WS_COMPRESSION` | `false` | Enable permessage-deflate on proxied WebSocket terminal traffic. Leave off unless clients connect over slow links — per-frame compression is CPU-expensive at high session counts. | | `TERMINALS_ACCESS_LOG` | `false` | Log every HTTP request. Off by default: at high request rates the per-request log record is measurable CPU. | | `TERMINALS_REPLAY_BODY_LIMIT` | | Maximum proxied request body bytes buffered for retry. Unset, `none`, `null`, or `unlimited` means no size cap. When set, larger known-size request bodies are streamed one-shot instead of buffered in orchestrator memory. | +| `TERMINALS_PROXY_CONNECT_TIMEOUT_SECONDS` | `10` | Timeout for opening upstream proxy connections to terminal containers. | +| `TERMINALS_PROXY_READ_TIMEOUT_SECONDS` | `360` | Timeout while waiting for upstream proxy responses. Keep above Open Terminal's maximum `/execute?wait` value. | +| `TERMINALS_PROXY_WRITE_TIMEOUT_SECONDS` | `300` | Timeout while sending proxied request bodies upstream. | +| `TERMINALS_PROXY_POOL_TIMEOUT_SECONDS` | `300` | Timeout while waiting for an upstream proxy connection from the pool. | See [`config.py`](terminals/config.py) for the full list. diff --git a/terminals/config.py b/terminals/config.py index 55a1cb3..7ca84dc 100644 --- a/terminals/config.py +++ b/terminals/config.py @@ -40,6 +40,10 @@ class Settings(BaseSettings): ws_compression: bool = False access_log: bool = False replay_body_limit: int | None = None + proxy_connect_timeout_seconds: float = 10 + proxy_read_timeout_seconds: float = 360 + proxy_write_timeout_seconds: float = 300 + proxy_pool_timeout_seconds: float = 300 @field_validator("replay_body_limit", mode="before") @classmethod diff --git a/terminals/routers/proxy.py b/terminals/routers/proxy.py index 679eed0..7b2eb55 100644 --- a/terminals/routers/proxy.py +++ b/terminals/routers/proxy.py @@ -48,7 +48,12 @@ async def _get_proxy_client(host: str, port: int) -> httpx.AsyncClient: client = _proxy_clients.pop((host, port), None) if client is None: client = httpx.AsyncClient( - timeout=httpx.Timeout(300.0, connect=10.0), + timeout=httpx.Timeout( + connect=settings.proxy_connect_timeout_seconds, + read=settings.proxy_read_timeout_seconds, + write=settings.proxy_write_timeout_seconds, + pool=settings.proxy_pool_timeout_seconds, + ), limits=httpx.Limits( max_connections=32, max_keepalive_connections=4, @@ -239,6 +244,17 @@ async def _proxy_request( status_code=502, media_type="application/json", ) + except httpx.ReadTimeout as e: + logger.warning( + "Proxy request to {} timed out waiting for upstream response: {}", + target_url, + e, + ) + return Response( + content='{"error": "Upstream response timed out"}', + status_code=504, + media_type="application/json", + ) except (httpx.RemoteProtocolError, httpx.ReadError) as e: request.app.state.backend.invalidate_status( user_id, policy_id, context_id