@@ -82,6 +82,23 @@ class ConfigDict(typing.TypedDict):
8282 connection_timeout_seconds (float): The connection timeout in seconds.
8383
8484 suppress_deprecation_warnings (bool): Whether to suppress deprecation warnings.
85+
86+ pool_timeout_seconds (float): How long a request waits for a free connection
87+ in the pool before raising ``httpx.PoolTimeout``. Defaults to
88+ ``connection_timeout_seconds``. Setting it lower than
89+ ``connection_timeout_seconds`` makes the httpcore connection leak
90+ (encode/httpcore#1093) more likely under load.
91+
92+ max_connections (int): The maximum number of connections in the pool.
93+ Defaults to 100.
94+
95+ max_keepalive_connections (int): The maximum number of idle connections
96+ kept alive in the pool. Defaults to 20.
97+
98+ max_concurrent_requests (int): The maximum number of requests in flight at
99+ once; further requests wait for a slot. Keep it below
100+ ``max_connections`` so a burst of slow requests cannot exhaust the pool.
101+ Defaults to no limit.
85102 """
86103
87104 nodes : typing .List [typing .Union [str , NodeConfigDict ]]
@@ -100,6 +117,10 @@ class ConfigDict(typing.TypedDict):
100117 ] # deprecated
101118 connection_timeout_seconds : typing .NotRequired [float ]
102119 suppress_deprecation_warnings : typing .NotRequired [bool ]
120+ pool_timeout_seconds : typing .NotRequired [float ]
121+ max_connections : typing .NotRequired [int ]
122+ max_keepalive_connections : typing .NotRequired [int ]
123+ max_concurrent_requests : typing .NotRequired [int ]
103124
104125
105126class Node :
@@ -188,6 +209,10 @@ class Configuration:
188209 retry_interval_seconds (float): The interval in seconds between retries.
189210 healthcheck_interval_seconds (int): The interval in seconds between health checks.
190211 verify (bool): Whether to verify the SSL certificate.
212+ pool_timeout_seconds (float): How long to wait for a free pooled connection.
213+ max_connections (int): The maximum number of connections in the pool.
214+ max_keepalive_connections (int): The maximum number of idle pooled connections.
215+ max_concurrent_requests (int | None): The maximum number of requests in flight.
191216 """
192217
193218 def __init__ (
@@ -232,6 +257,18 @@ def __init__(
232257 self .suppress_deprecation_warnings = config_dict .get (
233258 "suppress_deprecation_warnings" , False
234259 )
260+ self .pool_timeout_seconds = config_dict .get (
261+ "pool_timeout_seconds" ,
262+ self .connection_timeout_seconds ,
263+ )
264+ self .max_connections = config_dict .get ("max_connections" , 100 )
265+ self .max_keepalive_connections = config_dict .get (
266+ "max_keepalive_connections" ,
267+ 20 ,
268+ )
269+ self .max_concurrent_requests : typing .Optional [int ] = config_dict .get (
270+ "max_concurrent_requests" ,
271+ )
235272
236273 def _handle_nearest_node (
237274 self ,
@@ -295,6 +332,32 @@ def validate_config_dict(config_dict: ConfigDict) -> None:
295332 if nearest_node :
296333 ConfigurationValidations .validate_nearest_node (nearest_node )
297334
335+ ConfigurationValidations .validate_connection_pool (config_dict )
336+
337+ @staticmethod
338+ def validate_connection_pool (config_dict : ConfigDict ) -> None :
339+ """
340+ Validate the connection pool and concurrency settings.
341+
342+ Args:
343+ config_dict (ConfigDict): The configuration dictionary to validate.
344+
345+ Raises:
346+ ConfigError: If a pool or concurrency setting is out of range.
347+ """
348+ positive_settings : typing .Dict [str , typing .Optional [float ]] = {
349+ "pool_timeout_seconds" : config_dict .get ("pool_timeout_seconds" ),
350+ "max_connections" : config_dict .get ("max_connections" ),
351+ "max_concurrent_requests" : config_dict .get ("max_concurrent_requests" ),
352+ }
353+ for key , config_value in positive_settings .items ():
354+ if config_value is not None and config_value <= 0 :
355+ raise ConfigError (f"`{ key } ` must be greater than 0." )
356+
357+ max_keepalive_connections = config_dict .get ("max_keepalive_connections" )
358+ if max_keepalive_connections is not None and max_keepalive_connections < 0 :
359+ raise ConfigError ("`max_keepalive_connections` must not be negative." )
360+
298361 @staticmethod
299362 def validate_required_config_fields (config_dict : ConfigDict ) -> None :
300363 """
0 commit comments