merge fix/py-sdk-transport-singleton-lock into master
2 file(s) changed+76-6
Mpy-sdk/e2b/api/client_sync/__init__.py+12-6
| @@ -2,6 +2,7 @@ from typing import Optional | |||
| 2 | 2 | ||
| 3 | import httpx | 3 | import httpx |
| 4 | import logging | 4 | import logging |
| 5 | +import threading | ||
| 5 | 6 | ||
| 6 | from e2b.api import ApiClient, limits | 7 | from e2b.api import ApiClient, limits |
| 7 | from e2b.connection_config import ConnectionConfig | 8 | from e2b.connection_config import ConnectionConfig |
| @@ -36,15 +37,20 @@ class TransportWithLogger(httpx.HTTPTransport): | |||
| 36 | 37 | ||
| 37 | 38 | ||
| 38 | _transport: Optional[TransportWithLogger] = None | 39 | _transport: Optional[TransportWithLogger] = None |
| 40 | +_transport_lock = threading.Lock() | ||
| 39 | 41 | ||
| 40 | 42 | ||
| 41 | def get_transport(config: ConnectionConfig) -> TransportWithLogger: | 43 | def get_transport(config: ConnectionConfig) -> TransportWithLogger: |
| 42 | if TransportWithLogger.singleton is not None: | 44 | if TransportWithLogger.singleton is not None: |
| 43 | return TransportWithLogger.singleton | 45 | return TransportWithLogger.singleton |
| 44 | 46 | ||
| 45 | - transport = TransportWithLogger( | 47 | + # Serialize the first construction so concurrent clients share one pool. |
| 46 | - limits=limits, | 48 | + # Once published, the fast path above remains lock-free. |
| 47 | - proxy=config.proxy, | 49 | + with _transport_lock: |
| 48 | - ) | 50 | + if TransportWithLogger.singleton is None: |
| 49 | - TransportWithLogger.singleton = transport | 51 | + TransportWithLogger.singleton = TransportWithLogger( |
| 50 | - return transport | 52 | + limits=limits, |
| 53 | + proxy=config.proxy, | ||
| 54 | + ) | ||
| 55 | + | ||
| 56 | + return TransportWithLogger.singleton | ||
Apy-sdk/tests/unit/test_http_transport.py+64-0
Comments
Sign in to comment