Repository navigation
Add Trio backend #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Trio backend #143
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| if [ -n "$GITHUB_ACTIONS" ]; then | ||
| uv sync --locked | ||
| uv sync --locked --extra trio | ||
| else | ||
| uv sync | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| from __future__ import annotations | ||
|
|
||
|
|
||
| class AsyncNetworkStream: | ||
| async def read(self, size: int = -1) -> bytes: | ||
| raise NotImplementedError() # pragma: nocover | ||
|
|
||
| async def write(self, data: bytes) -> None: | ||
| raise NotImplementedError() # pragma: nocover | ||
|
|
||
| async def aclose(self) -> None: | ||
| raise NotImplementedError() # pragma: nocover | ||
|
|
||
|
|
||
| class AsyncNetworkBackend: | ||
| async def connect( | ||
| self, | ||
| host: str, | ||
| port: int, | ||
| ) -> AsyncNetworkStream: | ||
| raise NotImplementedError() # pragma: nocover | ||
|
|
||
| async def connect_tls( | ||
| self, host: str, port: int, hostname: str = "" | ||
| ) -> AsyncNetworkStream: | ||
| raise NotImplementedError() # pragma: nocover | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import ssl | ||
| import types | ||
| import typing | ||
|
|
||
| import trio | ||
| import truststore | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from typing import Self | ||
|
|
||
| from .base import AsyncNetworkBackend, AsyncNetworkStream | ||
|
|
||
| __all__ = ["TrioBackend", "TrioServer", "TrioStream"] | ||
|
|
||
|
|
||
| class TrioStream(AsyncNetworkStream): | ||
| def __init__(self, stream: trio.abc.Stream) -> None: | ||
| self._stream = stream | ||
| self._is_closed = False | ||
|
|
||
| async def read(self, size: int = -1) -> bytes: | ||
| if size == 0: | ||
| raise ValueError("Size must be greater than zero.") | ||
| max_bytes = 64 * 1024 if size < 0 else size | ||
| return bytes(await self._stream.receive_some(max_bytes=max_bytes)) | ||
|
|
||
| async def write(self, data: bytes) -> None: | ||
| if not data: | ||
| return | ||
| await self._stream.send_all(data) | ||
|
|
||
| async def aclose(self) -> None: | ||
| if not self._is_closed: | ||
| self._is_closed = True | ||
| with trio.CancelScope(shield=True): | ||
| await self._stream.aclose() | ||
|
|
||
| async def __aenter__(self) -> Self: | ||
| return self | ||
|
|
||
| async def __aexit__( | ||
| self, | ||
| exc_type: type[BaseException] | None = None, | ||
| exc_value: BaseException | None = None, | ||
| traceback: types.TracebackType | None = None, | ||
| ) -> None: | ||
| await self.aclose() | ||
|
|
||
| def __repr__(self) -> str: | ||
| description = " CLOSED" if self._is_closed else "" | ||
| return f"<TrioStream{description}>" | ||
|
|
||
|
|
||
| class TrioBackend(AsyncNetworkBackend): | ||
| def __init__(self, ssl_ctx: ssl.SSLContext | None = None) -> None: | ||
| self._ssl_ctx = self.create_default_context() if ssl_ctx is None else ssl_ctx | ||
|
|
||
| def create_default_context(self) -> ssl.SSLContext: | ||
| return truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT) | ||
|
|
||
| async def connect(self, host: str, port: int) -> TrioStream: | ||
| stream = await trio.open_tcp_stream(host, port) | ||
| return TrioStream(stream) | ||
|
|
||
| async def connect_tls(self, host: str, port: int, hostname: str = "") -> TrioStream: | ||
| tcp_stream = await trio.open_tcp_stream(host, port) | ||
| stream = trio.SSLStream( | ||
| tcp_stream, | ||
| ssl_context=self._ssl_ctx, | ||
| server_hostname=hostname or host, | ||
| https_compatible=True, | ||
| ) | ||
| try: | ||
| await stream.do_handshake() | ||
| except BaseException: | ||
| with trio.CancelScope(shield=True): | ||
| await stream.aclose() | ||
| raise # We'll want well defined exception semantics here. | ||
| return TrioStream(stream) | ||
|
|
||
| def serve( | ||
| self, | ||
| host: str, | ||
| port: int, | ||
| handler: typing.Callable[[TrioStream], typing.Awaitable[None]], | ||
| ) -> TrioServer: | ||
| return TrioServer(host, port, handler) | ||
|
|
||
| def __repr__(self) -> str: | ||
| return "<TrioBackend>" | ||
|
|
||
|
|
||
| class TrioServer: | ||
| def __init__( | ||
| self, | ||
| host: str, | ||
| port: int, | ||
| handler: typing.Callable[[TrioStream], typing.Awaitable[None]], | ||
| ) -> None: | ||
| self.host = host | ||
| self.port = port | ||
| self._handler = handler | ||
| self._listeners: list[trio.SocketListener] = [] | ||
| self._nursery_context: typing.Any = None | ||
| self._nursery: typing.Any = None | ||
|
|
||
| async def __aenter__(self) -> Self: | ||
| self._listeners = await trio.open_tcp_listeners(self.port, host=self.host) | ||
| self._nursery_context = trio.open_nursery() | ||
| self._nursery = await self._nursery_context.__aenter__() | ||
|
|
||
| for listener in self._listeners: | ||
| self._nursery.start_soon( | ||
| trio.serve_listeners, | ||
| self._handle_stream, | ||
| [listener], | ||
| ) | ||
|
|
||
| if self.port == 0 and self._listeners: | ||
| self.port = self._listeners[0].socket.getsockname()[1] | ||
| return self | ||
|
|
||
| async def __aexit__( | ||
| self, | ||
| exc_type: type[BaseException] | None = None, | ||
| exc_value: BaseException | None = None, | ||
| traceback: types.TracebackType | None = None, | ||
| ) -> bool | None: | ||
| self._nursery.cancel_scope.cancel() | ||
| try: | ||
| return await self._nursery_context.__aexit__(exc_type, exc_value, traceback) | ||
| finally: | ||
| with trio.CancelScope(shield=True): | ||
| for listener in self._listeners: | ||
| await listener.aclose() | ||
|
|
||
| async def _handle_stream(self, stream: trio.SocketStream) -> None: | ||
| trio_stream = TrioStream(stream) | ||
| try: | ||
| await self._handler(trio_stream) | ||
| finally: | ||
| await trio_stream.aclose() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
| import trio | ||
|
|
||
| from hip.backends.trio import TrioBackend, TrioStream | ||
|
|
||
|
|
||
| def test_connect_serve_context_managers(): | ||
| async def send_hello(stream): | ||
| await stream.write(b"hello") | ||
|
|
||
| async def run(): | ||
| backend = TrioBackend() | ||
| assert repr(backend) == "<TrioBackend>" | ||
|
|
||
| async with backend.serve("127.0.0.1", 0, send_hello) as server: | ||
| stream = await backend.connect(server.host, server.port) | ||
|
|
||
| async with stream: | ||
| with pytest.raises(ValueError, match="Size must be greater than zero"): | ||
| await stream.read(0) | ||
|
|
||
| received = b"" | ||
| while chunk := await stream.read(): | ||
| received += chunk | ||
| assert received == b"hello" | ||
|
|
||
| assert "CLOSED" in repr(stream) | ||
|
|
||
| trio.run(run) | ||
|
|
||
|
|
||
| def test_write_empty(): | ||
| async def run(): | ||
| transport = AsyncMock() | ||
| await TrioStream(transport).write(b"") | ||
| transport.send_all.assert_not_awaited() | ||
|
|
||
| trio.run(run) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we'll also want a well defined
NetworkErrorexception that can be raised by code in this module.We oughta spec the API so that the only expected failure modes are a
NetworkError. (Or aTimeoutif wrapped in a timeout context manager.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do that as a follow up maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah