Small speed up to writing outgoing packets (#785)

This commit is contained in:
J. Nick Koston 2023-12-04 15:59:33 -10:00 committed by GitHub
parent ac17f722c1
commit b22258cca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 14 deletions

View File

@ -5,7 +5,6 @@ from ..connection cimport APIConnection
cdef bint TYPE_CHECKING cdef bint TYPE_CHECKING
cdef object WRITE_EXCEPTIONS
cdef class APIFrameHelper: cdef class APIFrameHelper:

View File

@ -19,7 +19,6 @@ SOCKET_ERRORS = (
TimeoutError, TimeoutError,
) )
WRITE_EXCEPTIONS = (RuntimeError, ConnectionResetError, OSError)
_int = int _int = int
_bytes = bytes _bytes = bytes
@ -189,9 +188,4 @@ class APIFrameHelper:
if TYPE_CHECKING: if TYPE_CHECKING:
assert self._writer is not None, "Writer is not set" assert self._writer is not None, "Writer is not set"
try:
self._writer(data) self._writer(data)
except WRITE_EXCEPTIONS as err:
raise SocketClosedAPIError(
f"{self._log_name}: Error while writing data: {err}"
) from err

View File

@ -13,6 +13,8 @@ cdef object HANDSHAKE_TIMEOUT
cdef bint TYPE_CHECKING cdef bint TYPE_CHECKING
cdef object WRITE_EXCEPTIONS
cdef object DISCONNECT_REQUEST_MESSAGE cdef object DISCONNECT_REQUEST_MESSAGE
cdef tuple DISCONNECT_RESPONSE_MESSAGES cdef tuple DISCONNECT_RESPONSE_MESSAGES
cdef tuple PING_REQUEST_MESSAGES cdef tuple PING_REQUEST_MESSAGES
@ -46,6 +48,7 @@ cdef object ReadFailedAPIError
cdef object TimeoutAPIError cdef object TimeoutAPIError
cdef object SocketAPIError cdef object SocketAPIError
cdef object InvalidAuthAPIError cdef object InvalidAuthAPIError
cdef object SocketClosedAPIError
cdef object astuple cdef object astuple
@ -57,8 +60,8 @@ cdef object CONNECTION_STATE_CLOSED
cdef object make_hello_request cdef object make_hello_request
cpdef handle_timeout(object fut) cpdef void handle_timeout(object fut)
cpdef handle_complex_message( cpdef void handle_complex_message(
object fut, object fut,
list responses, list responses,
object do_append, object do_append,
@ -130,7 +133,7 @@ cdef class APIConnection:
cdef void _set_connection_state(self, object state) cdef void _set_connection_state(self, object state)
cpdef report_fatal_error(self, Exception err) cpdef void report_fatal_error(self, Exception err)
@cython.locals(handlers=set) @cython.locals(handlers=set)
cdef void _add_message_callback_without_remove(self, object on_message, tuple msg_types) cdef void _add_message_callback_without_remove(self, object on_message, tuple msg_types)

View File

@ -46,6 +46,7 @@ from .core import (
ReadFailedAPIError, ReadFailedAPIError,
ResolveAPIError, ResolveAPIError,
SocketAPIError, SocketAPIError,
SocketClosedAPIError,
TimeoutAPIError, TimeoutAPIError,
UnhandledAPIConnectionError, UnhandledAPIConnectionError,
) )
@ -95,6 +96,8 @@ CONNECT_REQUEST_TIMEOUT = 30.0
# to reboot and connect to the network/WiFi. # to reboot and connect to the network/WiFi.
TCP_CONNECT_TIMEOUT = 60.0 TCP_CONNECT_TIMEOUT = 60.0
WRITE_EXCEPTIONS = (RuntimeError, ConnectionResetError, OSError)
_int = int _int = int
_bytes = bytes _bytes = bytes
@ -667,12 +670,16 @@ class APIConnection:
try: try:
self._frame_helper.write_packets(packets, debug_enabled) self._frame_helper.write_packets(packets, debug_enabled)
except SocketAPIError as err: except WRITE_EXCEPTIONS as err:
# If writing packet fails, we don't know what state the frames # If writing packet fails, we don't know what state the frames
# are in anymore and we have to close the connection # are in anymore and we have to close the connection
_LOGGER.info("%s: Error writing packets: %s", self.log_name, err) _LOGGER.info("%s: Error writing packets: %s", self.log_name, err)
self.report_fatal_error(err) wrapped_err = SocketClosedAPIError(
raise f"{self.log_name}: Error writing packets: {err}"
)
wrapped_err.__cause__ = err
self.report_fatal_error(wrapped_err)
raise wrapped_err from err
def _add_message_callback_without_remove( def _add_message_callback_without_remove(
self, on_message: Callable[[Any], None], msg_types: tuple[type[Any], ...] self, on_message: Callable[[Any], None], msg_types: tuple[type[Any], ...]