Skip to content

Commit 90bbfdc

Browse files
Move connection poll check away from pool expiry checks
1 parent da86ca4 commit 90bbfdc

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

httpcore/_async/http11.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(
5959
) -> None:
6060
self._origin = origin
6161
self._network_stream = stream
62-
self._keepalive_expiry: Optional[float] = keepalive_expiry
62+
self._keepalive_expiry = keepalive_expiry
6363
self._expire_at: Optional[float] = None
6464
self._state = HTTPConnectionState.NEW
6565
self._state_lock = AsyncLock()
@@ -77,6 +77,16 @@ async def handle_async_request(self, request: Request) -> Response:
7777
)
7878

7979
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+
8090
if self._state in (HTTPConnectionState.NEW, HTTPConnectionState.IDLE):
8191
self._request_count += 1
8292
self._state = HTTPConnectionState.ACTIVE
@@ -280,17 +290,7 @@ def is_available(self) -> bool:
280290

281291
def has_expired(self) -> bool:
282292
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
294294

295295
def is_idle(self) -> bool:
296296
return self._state == HTTPConnectionState.IDLE

httpcore/_sync/http11.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(
5959
) -> None:
6060
self._origin = origin
6161
self._network_stream = stream
62-
self._keepalive_expiry: Optional[float] = keepalive_expiry
62+
self._keepalive_expiry = keepalive_expiry
6363
self._expire_at: Optional[float] = None
6464
self._state = HTTPConnectionState.NEW
6565
self._state_lock = Lock()
@@ -77,6 +77,16 @@ def handle_request(self, request: Request) -> Response:
7777
)
7878

7979
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+
8090
if self._state in (HTTPConnectionState.NEW, HTTPConnectionState.IDLE):
8191
self._request_count += 1
8292
self._state = HTTPConnectionState.ACTIVE
@@ -280,17 +290,7 @@ def is_available(self) -> bool:
280290

281291
def has_expired(self) -> bool:
282292
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
294294

295295
def is_idle(self) -> bool:
296296
return self._state == HTTPConnectionState.IDLE

0 commit comments

Comments
 (0)