33"""
44
55from json import JSONDecodeError
6- from httpx import Response , AsyncClient , TimeoutException , ConnectError
6+ from httpx import Response , AsyncClient , TimeoutException , ConnectError , Limits
77from typing import Any , Dict , Optional
88from ..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 ,
0 commit comments