Skip to content

Commit 0d5b2b7

Browse files
committed
Add connection limits to AsyncHTTPClient and HTTPClient for improved concurrency handling
1 parent dbefdd2 commit 0d5b2b7

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

recallrai/utils/async_http_client.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from json import JSONDecodeError
6-
from httpx import Response, AsyncClient, TimeoutException, ConnectError
6+
from httpx import Response, AsyncClient, TimeoutException, ConnectError, Limits
77
from typing import Any, Dict, Optional
88
from ..exceptions import (
99
TimeoutError,
@@ -42,8 +42,16 @@ def __init__(
4242

4343
async def __aenter__(self):
4444
"""Async context manager entry."""
45+
# Configure connection limits to handle concurrent requests better
46+
# Default httpx limits can cause connection errors under high concurrency
47+
limits = Limits(
48+
max_connections=100, # Maximum total connections
49+
max_keepalive_connections=20, # Maximum idle connections to keep alive
50+
)
51+
4552
self._client = AsyncClient(
4653
timeout=self.timeout,
54+
limits=limits,
4755
headers={
4856
"X-Recallr-Api-Key": self.api_key,
4957
"X-Recallr-Project-Id": self.project_id,
@@ -62,8 +70,15 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
6270
async def _ensure_client(self):
6371
"""Ensure the client is initialized."""
6472
if self._client is None:
73+
# Configure connection limits to handle concurrent requests better
74+
limits = Limits(
75+
max_connections=100,
76+
max_keepalive_connections=20,
77+
)
78+
6579
self._client = AsyncClient(
6680
timeout=self.timeout,
81+
limits=limits,
6782
headers={
6883
"X-Recallr-Api-Key": self.api_key,
6984
"X-Recallr-Project-Id": self.project_id,

recallrai/utils/http_client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from json import JSONDecodeError
6-
from httpx import Response, Client, TimeoutException, ConnectError
6+
from httpx import Response, Client, TimeoutException, ConnectError, Limits
77
from typing import Any, Dict, Optional
88
from ..exceptions import (
99
TimeoutError,
@@ -37,8 +37,16 @@ def __init__(
3737
self.project_id = project_id
3838
self.base_url = base_url.rstrip("/")
3939
self.timeout = timeout
40+
41+
# Configure connection limits to handle concurrent requests better
42+
limits = Limits(
43+
max_connections=100,
44+
max_keepalive_connections=20,
45+
)
46+
4047
self.client = Client(
4148
timeout=self.timeout,
49+
limits=limits,
4250
headers={
4351
"X-Recallr-Api-Key": self.api_key,
4452
"X-Recallr-Project-Id": self.project_id,

0 commit comments

Comments
 (0)