Skip to content

Commit 5601f19

Browse files
Copilotcgillum
andcommitted
Optimize timeout and connector settings for localhost IPC and add session locks
Co-authored-by: cgillum <2704139+cgillum@users.noreply.github.com>
1 parent cf2ebe0 commit 5601f19

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

azure/durable_functions/models/utils/http_utils.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,17 @@ async def _get_session() -> aiohttp.ClientSession:
2424
async with _session_lock:
2525
# Check again after acquiring lock
2626
if _client_session is None or _client_session.closed:
27-
# Configure timeout and connection pooling
27+
# Configure timeout optimized for localhost IPC
2828
timeout = aiohttp.ClientTimeout(
29-
total=60, # Total timeout for entire request
30-
connect=30, # Timeout for connection establishment
31-
sock_connect=30, # Socket connection timeout
32-
sock_read=30 # Socket read timeout
29+
total=240, # 4-minute total timeout for slow operations
30+
sock_connect=10, # Fast connection over localhost
31+
sock_read=None # Covered by total timeout
3332
)
3433

35-
# Configure TCP connector with connection pooling
34+
# Configure TCP connector optimized for localhost IPC
3635
connector = aiohttp.TCPConnector(
37-
limit=100, # Maximum number of connections
36+
limit=30, # Maximum connections for single host
3837
limit_per_host=30, # Maximum connections per host
39-
ttl_dns_cache=300, # DNS cache TTL in seconds
4038
enable_cleanup_closed=True # Enable cleanup of closed connections
4139
)
4240

@@ -54,9 +52,10 @@ async def _handle_request_error():
5452
This handles cases where the remote host process recycles.
5553
"""
5654
global _client_session
57-
if _client_session is not None and not _client_session.closed:
58-
await _client_session.close()
59-
_client_session = None
55+
async with _session_lock:
56+
if _client_session is not None and not _client_session.closed:
57+
await _client_session.close()
58+
_client_session = None
6059

6160

6261
async def _close_session() -> None:
@@ -66,9 +65,10 @@ async def _close_session() -> None:
6665
"""
6766
global _client_session
6867

69-
if _client_session is not None and not _client_session.closed:
70-
await _client_session.close()
71-
_client_session = None
68+
async with _session_lock:
69+
if _client_session is not None and not _client_session.closed:
70+
await _client_session.close()
71+
_client_session = None
7272

7373

7474
async def post_async_request(url: str,

tests/utils/test_http_utils.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,17 @@ async def test_session_configured_with_timeouts():
205205
await http_utils.post_async_request("http://test.com",
206206
{"data": "test"})
207207

208-
# Verify timeout was configured
208+
# Verify timeout was configured for localhost IPC
209209
mock_timeout_class.assert_called_once()
210210
timeout_call = mock_timeout_class.call_args
211-
assert timeout_call.kwargs['total'] == 60
212-
assert timeout_call.kwargs['connect'] == 30
213-
assert timeout_call.kwargs['sock_connect'] == 30
214-
assert timeout_call.kwargs['sock_read'] == 30
211+
assert timeout_call.kwargs['total'] == 240
212+
assert timeout_call.kwargs['sock_connect'] == 10
213+
assert timeout_call.kwargs['sock_read'] is None
215214

216-
# Verify connector was configured
215+
# Verify connector was configured for localhost IPC
217216
mock_connector_class.assert_called_once()
218217
connector_call = mock_connector_class.call_args
219-
assert connector_call.kwargs['limit'] == 100
218+
assert connector_call.kwargs['limit'] == 30
220219
assert connector_call.kwargs['limit_per_host'] == 30
221220

222221

0 commit comments

Comments
 (0)