Skip to content

Commit

Permalink
Add to state enum
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusSintonen committed Jun 15, 2024
1 parent abd7b13 commit 4254af5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
11 changes: 7 additions & 4 deletions httpcore/_async/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class HTTPConnectionState(enum.IntEnum):
ACTIVE = 1
IDLE = 2
CLOSED = 3
SERVER_DISCONNECTED = 4


class AsyncHTTP11Connection(AsyncConnectionInterface):
Expand All @@ -63,7 +64,6 @@ def __init__(
self._keepalive_expiry = keepalive_expiry
self._expire_at: Optional[float] = None
self._state = HTTPConnectionState.NEW
self._server_disconnected = False
self._state_lock = AsyncLock()
self._request_count = 0
self._h11_state = h11.Connection(
Expand All @@ -86,8 +86,11 @@ async def handle_async_request(self, request: Request) -> Response:
self._state == HTTPConnectionState.IDLE
and self._network_stream.get_extra_info("is_readable")
)
if server_disconnected or self._server_disconnected:
self._server_disconnected = True
if (
server_disconnected
or self._state == HTTPConnectionState.SERVER_DISCONNECTED
):
self._state = HTTPConnectionState.SERVER_DISCONNECTED
raise ServerDisconnectedInternalError()

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

def has_expired(self) -> bool:
if self._server_disconnected:
if self._state == HTTPConnectionState.SERVER_DISCONNECTED:
# Connection that is disconnected by the server is considered expired.
# Pool then cleans up this connection by closing it.
return True
Expand Down
11 changes: 7 additions & 4 deletions httpcore/_sync/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class HTTPConnectionState(enum.IntEnum):
ACTIVE = 1
IDLE = 2
CLOSED = 3
SERVER_DISCONNECTED = 4


class HTTP11Connection(ConnectionInterface):
Expand All @@ -63,7 +64,6 @@ def __init__(
self._keepalive_expiry = keepalive_expiry
self._expire_at: Optional[float] = None
self._state = HTTPConnectionState.NEW
self._server_disconnected = False
self._state_lock = Lock()
self._request_count = 0
self._h11_state = h11.Connection(
Expand All @@ -86,8 +86,11 @@ def handle_request(self, request: Request) -> Response:
self._state == HTTPConnectionState.IDLE
and self._network_stream.get_extra_info("is_readable")
)
if server_disconnected or self._server_disconnected:
self._server_disconnected = True
if (
server_disconnected
or self._state == HTTPConnectionState.SERVER_DISCONNECTED
):
self._state = HTTPConnectionState.SERVER_DISCONNECTED
raise ServerDisconnectedInternalError()

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

def has_expired(self) -> bool:
if self._server_disconnected:
if self._state == HTTPConnectionState.SERVER_DISCONNECTED:
# Connection that is disconnected by the server is considered expired.
# Pool then cleans up this connection by closing it.
return True
Expand Down
4 changes: 3 additions & 1 deletion tests/_async/test_http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ def get_extra_info(self, info: str) -> typing.Any:
async with httpcore.AsyncHTTP11Connection(origin=origin, stream=stream) as conn:
await conn.request("GET", "https://example.com/")

assert conn.is_idle()
assert conn.is_idle() and not conn.has_expired()
stream.mock_is_readable = True # Simulate connection breakage

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

assert conn.has_expired() and not conn.is_idle()


@pytest.mark.anyio
async def test_http11_connection_attempt_close():
Expand Down
4 changes: 3 additions & 1 deletion tests/_sync/test_http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ def get_extra_info(self, info: str) -> typing.Any:
with httpcore.HTTP11Connection(origin=origin, stream=stream) as conn:
conn.request("GET", "https://example.com/")

assert conn.is_idle()
assert conn.is_idle() and not conn.has_expired()
stream.mock_is_readable = True # Simulate connection breakage

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

assert conn.has_expired() and not conn.is_idle()



def test_http11_connection_attempt_close():
Expand Down

0 comments on commit 4254af5

Please sign in to comment.