|
| 1 | +"""HTTP/1.1 transports for Jupyter requests. |
| 2 | +
|
| 3 | +# TODO: Remove later |
| 4 | +The base SDK's shared transports let ALPN negotiate the HTTP version, which |
| 5 | +means HTTP/2 against the sandbox. With HTTP/2, multiple requests are |
| 6 | +multiplexed over a single TCP connection, so when a client cancels a request |
| 7 | +(e.g. the caller disconnects from the streaming `/execute` endpoint) the |
| 8 | +server may not detect the disconnect: only the HTTP/2 stream is cancelled, |
| 9 | +the underlying TCP connection stays open. |
| 10 | +
|
| 11 | +Forcing HTTP/1.1 keeps the 1:1 mapping between TCP connection and request, so |
| 12 | +client disconnects propagate to the server as a TCP close, uvicorn delivers |
| 13 | +`http.disconnect`, and long-running executions can be cancelled reliably. |
| 14 | +
|
| 15 | +The base SDK has no option for this, so we build the transport ourselves from |
| 16 | +the same pieces it uses (its pool tuning and connect-only retry policy) with |
| 17 | +`http_version` pinned to HTTP/1.1. |
| 18 | +""" |
| 19 | + |
| 20 | +import threading |
| 21 | +from typing import Dict, Optional |
| 22 | + |
| 23 | +from pyqwest import HTTPTransport, HTTPVersion, SyncHTTPTransport |
| 24 | +from pyqwest.httpx import AsyncPyqwestTransport, PyqwestTransport |
| 25 | + |
| 26 | +from e2b.api import ( |
| 27 | + ProxyConfig, |
| 28 | + connection_retries, |
| 29 | + pool_idle_timeout, |
| 30 | + pool_max_idle_per_host, |
| 31 | + proxy_to_config, |
| 32 | +) |
| 33 | +from e2b.api.client_async import ( |
| 34 | + ConnectionRetryTransport as AsyncConnectionRetryTransport, |
| 35 | +) |
| 36 | +from e2b.api.client_sync import ConnectionRetryTransport as SyncConnectionRetryTransport |
| 37 | +from e2b.connection_config import ConnectionConfig |
| 38 | + |
| 39 | +_transport_lock = threading.Lock() |
| 40 | +# One transport (= one connection pool) per proxy; None is the direct pool. |
| 41 | +# pyqwest transports are thread-safe and loop-independent, so the caches are |
| 42 | +# process-global rather than per-thread (sync) or per-event-loop (async). |
| 43 | +_sync_transports: Dict[Optional[ProxyConfig], PyqwestTransport] = {} |
| 44 | +_async_transports: Dict[Optional[ProxyConfig], AsyncPyqwestTransport] = {} |
| 45 | + |
| 46 | + |
| 47 | +def _transport_kwargs(proxy: Optional[ProxyConfig]) -> dict: |
| 48 | + return dict( |
| 49 | + # System CA certs, without which TLS through an intercepting proxy |
| 50 | + # fails. |
| 51 | + tls_include_system_certs=True, |
| 52 | + proxy=proxy.to_pyqwest() if proxy is not None else None, |
| 53 | + http_version=HTTPVersion.HTTP1, |
| 54 | + pool_idle_timeout=pool_idle_timeout, |
| 55 | + pool_max_idle_per_host=pool_max_idle_per_host, |
| 56 | + # Redirects belong to the httpx client above, not to reqwest. |
| 57 | + follow_redirects=False, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def get_sync_transport(config: ConnectionConfig) -> PyqwestTransport: |
| 62 | + """The shared HTTP/1.1 transport for synchronous Jupyter requests.""" |
| 63 | + proxy = proxy_to_config(config.proxy) |
| 64 | + with _transport_lock: |
| 65 | + transport = _sync_transports.get(proxy) |
| 66 | + if transport is None: |
| 67 | + transport = PyqwestTransport( |
| 68 | + SyncConnectionRetryTransport( |
| 69 | + SyncHTTPTransport(**_transport_kwargs(proxy)), |
| 70 | + max_retries=connection_retries, |
| 71 | + ) |
| 72 | + ) |
| 73 | + _sync_transports[proxy] = transport |
| 74 | + return transport |
| 75 | + |
| 76 | + |
| 77 | +def get_async_transport(config: ConnectionConfig) -> AsyncPyqwestTransport: |
| 78 | + """The shared HTTP/1.1 transport for asynchronous Jupyter requests.""" |
| 79 | + proxy = proxy_to_config(config.proxy) |
| 80 | + with _transport_lock: |
| 81 | + transport = _async_transports.get(proxy) |
| 82 | + if transport is None: |
| 83 | + transport = AsyncPyqwestTransport( |
| 84 | + AsyncConnectionRetryTransport( |
| 85 | + HTTPTransport(**_transport_kwargs(proxy)), |
| 86 | + max_retries=connection_retries, |
| 87 | + ) |
| 88 | + ) |
| 89 | + _async_transports[proxy] = transport |
| 90 | + return transport |
0 commit comments