Skip to content

Commit 4254af5

Browse files
Add to state enum
1 parent abd7b13 commit 4254af5

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

httpcore/_async/http11.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class HTTPConnectionState(enum.IntEnum):
4646
ACTIVE = 1
4747
IDLE = 2
4848
CLOSED = 3
49+
SERVER_DISCONNECTED = 4
4950

5051

5152
class AsyncHTTP11Connection(AsyncConnectionInterface):
@@ -63,7 +64,6 @@ def __init__(
6364
self._keepalive_expiry = keepalive_expiry
6465
self._expire_at: Optional[float] = None
6566
self._state = HTTPConnectionState.NEW
66-
self._server_disconnected = False
6767
self._state_lock = AsyncLock()
6868
self._request_count = 0
6969
self._h11_state = h11.Connection(
@@ -86,8 +86,11 @@ async def handle_async_request(self, request: Request) -> Response:
8686
self._state == HTTPConnectionState.IDLE
8787
and self._network_stream.get_extra_info("is_readable")
8888
)
89-
if server_disconnected or self._server_disconnected:
90-
self._server_disconnected = True
89+
if (
90+
server_disconnected
91+
or self._state == HTTPConnectionState.SERVER_DISCONNECTED
92+
):
93+
self._state = HTTPConnectionState.SERVER_DISCONNECTED
9194
raise ServerDisconnectedInternalError()
9295

9396
if self._state in (HTTPConnectionState.NEW, HTTPConnectionState.IDLE):
@@ -292,7 +295,7 @@ def is_available(self) -> bool:
292295
return self._state == HTTPConnectionState.IDLE
293296

294297
def has_expired(self) -> bool:
295-
if self._server_disconnected:
298+
if self._state == HTTPConnectionState.SERVER_DISCONNECTED:
296299
# Connection that is disconnected by the server is considered expired.
297300
# Pool then cleans up this connection by closing it.
298301
return True

httpcore/_sync/http11.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class HTTPConnectionState(enum.IntEnum):
4646
ACTIVE = 1
4747
IDLE = 2
4848
CLOSED = 3
49+
SERVER_DISCONNECTED = 4
4950

5051

5152
class HTTP11Connection(ConnectionInterface):
@@ -63,7 +64,6 @@ def __init__(
6364
self._keepalive_expiry = keepalive_expiry
6465
self._expire_at: Optional[float] = None
6566
self._state = HTTPConnectionState.NEW
66-
self._server_disconnected = False
6767
self._state_lock = Lock()
6868
self._request_count = 0
6969
self._h11_state = h11.Connection(
@@ -86,8 +86,11 @@ def handle_request(self, request: Request) -> Response:
8686
self._state == HTTPConnectionState.IDLE
8787
and self._network_stream.get_extra_info("is_readable")
8888
)
89-
if server_disconnected or self._server_disconnected:
90-
self._server_disconnected = True
89+
if (
90+
server_disconnected
91+
or self._state == HTTPConnectionState.SERVER_DISCONNECTED
92+
):
93+
self._state = HTTPConnectionState.SERVER_DISCONNECTED
9194
raise ServerDisconnectedInternalError()
9295

9396
if self._state in (HTTPConnectionState.NEW, HTTPConnectionState.IDLE):
@@ -292,7 +295,7 @@ def is_available(self) -> bool:
292295
return self._state == HTTPConnectionState.IDLE
293296

294297
def has_expired(self) -> bool:
295-
if self._server_disconnected:
298+
if self._state == HTTPConnectionState.SERVER_DISCONNECTED:
296299
# Connection that is disconnected by the server is considered expired.
297300
# Pool then cleans up this connection by closing it.
298301
return True

tests/_async/test_http11.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,14 @@ def get_extra_info(self, info: str) -> typing.Any:
199199
async with httpcore.AsyncHTTP11Connection(origin=origin, stream=stream) as conn:
200200
await conn.request("GET", "https://example.com/")
201201

202-
assert conn.is_idle()
202+
assert conn.is_idle() and not conn.has_expired()
203203
stream.mock_is_readable = True # Simulate connection breakage
204204

205205
with pytest.raises(ServerDisconnectedInternalError):
206206
await conn.request("GET", "https://example.com/")
207207

208+
assert conn.has_expired() and not conn.is_idle()
209+
208210

209211
@pytest.mark.anyio
210212
async def test_http11_connection_attempt_close():

tests/_sync/test_http11.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,14 @@ def get_extra_info(self, info: str) -> typing.Any:
199199
with httpcore.HTTP11Connection(origin=origin, stream=stream) as conn:
200200
conn.request("GET", "https://example.com/")
201201

202-
assert conn.is_idle()
202+
assert conn.is_idle() and not conn.has_expired()
203203
stream.mock_is_readable = True # Simulate connection breakage
204204

205205
with pytest.raises(ServerDisconnectedInternalError):
206206
conn.request("GET", "https://example.com/")
207207

208+
assert conn.has_expired() and not conn.is_idle()
209+
208210

209211

210212
def test_http11_connection_attempt_close():

0 commit comments

Comments
 (0)