@@ -52,6 +52,7 @@ def __init__(
5252 AutoBackend () if network_backend is None else network_backend
5353 )
5454 self ._connection : Optional [AsyncConnectionInterface ] = None
55+ self ._connect_failed : bool = False
5556 self ._request_lock = AsyncLock ()
5657
5758 async def handle_async_request (self , request : Request ) -> Response :
@@ -62,27 +63,31 @@ async def handle_async_request(self, request: Request) -> Response:
6263
6364 async with self ._request_lock :
6465 if self ._connection is None :
65- stream = await self ._connect (request )
66-
67- ssl_object = stream .get_extra_info ("ssl_object" )
68- http2_negotiated = (
69- ssl_object is not None
70- and ssl_object .selected_alpn_protocol () == "h2"
71- )
72- if http2_negotiated or (self ._http2 and not self ._http1 ):
73- from .http2 import AsyncHTTP2Connection
74-
75- self ._connection = AsyncHTTP2Connection (
76- origin = self ._origin ,
77- stream = stream ,
78- keepalive_expiry = self ._keepalive_expiry ,
79- )
80- else :
81- self ._connection = AsyncHTTP11Connection (
82- origin = self ._origin ,
83- stream = stream ,
84- keepalive_expiry = self ._keepalive_expiry ,
66+ try :
67+ stream = await self ._connect (request )
68+
69+ ssl_object = stream .get_extra_info ("ssl_object" )
70+ http2_negotiated = (
71+ ssl_object is not None
72+ and ssl_object .selected_alpn_protocol () == "h2"
8573 )
74+ if http2_negotiated or (self ._http2 and not self ._http1 ):
75+ from .http2 import AsyncHTTP2Connection
76+
77+ self ._connection = AsyncHTTP2Connection (
78+ origin = self ._origin ,
79+ stream = stream ,
80+ keepalive_expiry = self ._keepalive_expiry ,
81+ )
82+ else :
83+ self ._connection = AsyncHTTP11Connection (
84+ origin = self ._origin ,
85+ stream = stream ,
86+ keepalive_expiry = self ._keepalive_expiry ,
87+ )
88+ except Exception as exc :
89+ self ._connect_failed = True
90+ raise exc
8691 elif not self ._connection .is_available ():
8792 raise ConnectionNotAvailable ()
8893
@@ -154,27 +159,31 @@ def is_available(self) -> bool:
154159 # If HTTP/2 support is enabled, and the resulting connection could
155160 # end up as HTTP/2 then we should indicate the connection as being
156161 # available to service multiple requests.
157- return self ._http2 and (self ._origin .scheme == b"https" or not self ._http1 )
162+ return (
163+ self ._http2
164+ and (self ._origin .scheme == b"https" or not self ._http1 )
165+ and not self ._connect_failed
166+ )
158167 return self ._connection .is_available ()
159168
160169 def has_expired (self ) -> bool :
161170 if self ._connection is None :
162- return False
171+ return self . _connect_failed
163172 return self ._connection .has_expired ()
164173
165174 def is_idle (self ) -> bool :
166175 if self ._connection is None :
167- return False
176+ return self . _connect_failed
168177 return self ._connection .is_idle ()
169178
170179 def is_closed (self ) -> bool :
171180 if self ._connection is None :
172- return False
181+ return self . _connect_failed
173182 return self ._connection .is_closed ()
174183
175184 def info (self ) -> str :
176185 if self ._connection is None :
177- return "CONNECTING"
186+ return "CONNECTION FAILED" if self . _connect_failed else " CONNECTING"
178187 return self ._connection .info ()
179188
180189 def __repr__ (self ) -> str :
0 commit comments