@@ -59,7 +59,7 @@ def __init__(
59
59
) -> None :
60
60
self ._origin = origin
61
61
self ._network_stream = stream
62
- self ._keepalive_expiry : Optional [ float ] = keepalive_expiry
62
+ self ._keepalive_expiry = keepalive_expiry
63
63
self ._expire_at : Optional [float ] = None
64
64
self ._state = HTTPConnectionState .NEW
65
65
self ._state_lock = AsyncLock ()
@@ -77,6 +77,16 @@ async def handle_async_request(self, request: Request) -> Response:
77
77
)
78
78
79
79
async with self ._state_lock :
80
+ # If the HTTP connection is idle but the socket is readable, then the
81
+ # only valid state is that the socket is about to return b"", indicating
82
+ # a server-initiated disconnect.
83
+ server_disconnected = (
84
+ self ._state == HTTPConnectionState .IDLE
85
+ and self ._network_stream .get_extra_info ("is_readable" )
86
+ )
87
+ if server_disconnected :
88
+ raise ConnectionNotAvailable ()
89
+
80
90
if self ._state in (HTTPConnectionState .NEW , HTTPConnectionState .IDLE ):
81
91
self ._request_count += 1
82
92
self ._state = HTTPConnectionState .ACTIVE
@@ -280,17 +290,7 @@ def is_available(self) -> bool:
280
290
281
291
def has_expired (self ) -> bool :
282
292
now = time .monotonic ()
283
- keepalive_expired = self ._expire_at is not None and now > self ._expire_at
284
-
285
- # If the HTTP connection is idle but the socket is readable, then the
286
- # only valid state is that the socket is about to return b"", indicating
287
- # a server-initiated disconnect.
288
- server_disconnected = (
289
- self ._state == HTTPConnectionState .IDLE
290
- and self ._network_stream .get_extra_info ("is_readable" )
291
- )
292
-
293
- return keepalive_expired or server_disconnected
293
+ return self ._expire_at is not None and now > self ._expire_at
294
294
295
295
def is_idle (self ) -> bool :
296
296
return self ._state == HTTPConnectionState .IDLE
0 commit comments