Skip to content

Commit

Permalink
Allow the asyncio protocol to specify the close code / reason
Browse files Browse the repository at this point in the history
The asyncio wrapper for the `close` method should allow the API user to
specify the error code and the reason phrase.
  • Loading branch information
jlaine committed Jul 6, 2024
1 parent 6987588 commit 7ad382f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/http3_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ async def main(

# process http pushes
process_http_pushes(client=client, include=include, output_dir=output_dir)
client._quic.close(error_code=ErrorCode.H3_NO_ERROR)
client.close(error_code=ErrorCode.H3_NO_ERROR)


if __name__ == "__main__":
Expand Down
17 changes: 15 additions & 2 deletions src/aioquic/asyncio/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ..quic import events
from ..quic.connection import NetworkAddress, QuicConnection
from ..quic.packet import QuicErrorCode

QuicConnectionIdHandler = Callable[[bytes], None]
QuicStreamHandler = Callable[[asyncio.StreamReader, asyncio.StreamWriter], None]
Expand Down Expand Up @@ -44,11 +45,23 @@ def change_connection_id(self) -> None:
self._quic.change_connection_id()
self.transmit()

def close(self) -> None:
def close(
self,
error_code: int = QuicErrorCode.NO_ERROR,
reason_phrase: str = "",
) -> None:
"""
Close the connection.
:param error_code: An error code indicating why the connection is
being closed.
:param reason_phrase: A human-readable explanation of why the
connection is being closed.
"""
self._quic.close()
self._quic.close(
error_code=error_code,
reason_phrase=reason_phrase,
)
self.transmit()

def connect(self, addr: NetworkAddress) -> None:
Expand Down

0 comments on commit 7ad382f

Please sign in to comment.