3434import asyncio
3535import logging
3636import threading
37+
3738from dataclasses import dataclass , field
38- from typing import Any , ClassVar
39+ from typing import Any , ClassVar , Self
3940
4041import httpx
4142
@@ -112,9 +113,7 @@ class HttpClientPool:
112113
113114 @classmethod
114115 def get_instance (
115- cls ,
116- limits : PoolLimits | None = None ,
117- timeouts : PoolTimeouts | None = None ,
116+ cls , limits : PoolLimits | None = None , timeouts : PoolTimeouts | None = None
118117 ) -> HttpClientPool :
119118 """Get or create the singleton HttpClientPool instance.
120119
@@ -135,8 +134,7 @@ def get_instance(
135134 with _instance_lock :
136135 if cls ._instance is None :
137136 cls ._instance = cls (
138- limits = limits or PoolLimits (),
139- timeouts = timeouts or PoolTimeouts (),
137+ limits = limits or PoolLimits (), timeouts = timeouts or PoolTimeouts ()
140138 )
141139 logger .debug (
142140 "Created HttpClientPool singleton with limits=%s, timeouts=%s" ,
@@ -205,8 +203,7 @@ async def get_client(self, name: str, **overrides: Any) -> httpx.AsyncClient:
205203 limits = httpx .Limits (
206204 max_connections = overrides .get ("max_connections" , self .limits .max_connections ),
207205 max_keepalive_connections = overrides .get (
208- "max_keepalive_connections" ,
209- self .limits .max_keepalive_connections ,
206+ "max_keepalive_connections" , self .limits .max_keepalive_connections
210207 ),
211208 keepalive_expiry = overrides .get ("keepalive_expiry" , self .limits .keepalive_expiry ),
212209 )
@@ -275,10 +272,11 @@ def get_client_sync(self, name: str, **overrides: Any) -> httpx.AsyncClient:
275272 limits = httpx .Limits (
276273 max_connections = overrides .get ("max_connections" , self .limits .max_connections ),
277274 max_keepalive_connections = overrides .get (
278- "max_keepalive_connections" ,
279- self .limits .max_keepalive_connections ,
275+ "max_keepalive_connections" , self .limits .max_keepalive_connections
276+ ),
277+ keepalive_expiry = overrides .get (
278+ "keepalive_expiry" , self .limits .keepalive_expiry
280279 ),
281- keepalive_expiry = overrides .get ("keepalive_expiry" , self .limits .keepalive_expiry ),
282280 )
283281
284282 timeout = httpx .Timeout (
@@ -289,9 +287,7 @@ def get_client_sync(self, name: str, **overrides: Any) -> httpx.AsyncClient:
289287 )
290288
291289 self ._clients [name ] = httpx .AsyncClient (
292- limits = limits ,
293- timeout = timeout ,
294- http2 = overrides .get ("http2" , True ),
290+ limits = limits , timeout = timeout , http2 = overrides .get ("http2" , True )
295291 )
296292
297293 logger .debug (
@@ -341,10 +337,11 @@ async def close_client(self, name: str) -> bool:
341337 try :
342338 await client .aclose ()
343339 logger .debug ("Closed HTTP client pool for %s" , name )
344- return True
345340 except (httpx .HTTPError , OSError ) as e :
346341 logger .warning ("Error closing HTTP client pool for %s: %s" , name , e )
347342 # Client already removed from dict above
343+ else :
344+ return True
348345 return False
349346
350347 async def close_all (self ) -> None :
@@ -372,11 +369,11 @@ async def close_all(self) -> None:
372369 if client_names :
373370 logger .debug ("Closed %d HTTP client pool(s)" , len (client_names ))
374371
375- async def __aenter__ (self ) -> HttpClientPool :
372+ async def __aenter__ (self ) -> Self :
376373 """Async context manager entry."""
377374 return self
378375
379- async def __aexit__ (self , * args : Any ) -> None :
376+ async def __aexit__ (self , * args : object ) -> None :
380377 """Async context manager exit - closes all clients."""
381378 await self .close_all ()
382379
0 commit comments