Small speed up to writing outgoing packets
There were two try blocks to convert exception types. Simplify by removing one of the try blocks and converting the exceptions in the connection class
This commit is contained in:
parent
66f9341fe5
commit
488443c70a
|
@ -189,9 +189,4 @@ class APIFrameHelper:
|
|||
if TYPE_CHECKING:
|
||||
assert self._writer is not None, "Writer is not set"
|
||||
|
||||
try:
|
||||
self._writer(data)
|
||||
except WRITE_EXCEPTIONS as err:
|
||||
raise SocketClosedAPIError(
|
||||
f"{self._log_name}: Error while writing data: {err}"
|
||||
) from err
|
||||
self._writer(data)
|
||||
|
|
|
@ -13,6 +13,8 @@ cdef object HANDSHAKE_TIMEOUT
|
|||
|
||||
cdef bint TYPE_CHECKING
|
||||
|
||||
cdef object WRITE_EXCEPTIONS
|
||||
|
||||
cdef object DISCONNECT_REQUEST_MESSAGE
|
||||
cdef tuple DISCONNECT_RESPONSE_MESSAGES
|
||||
cdef tuple PING_REQUEST_MESSAGES
|
||||
|
@ -46,6 +48,7 @@ cdef object ReadFailedAPIError
|
|||
cdef object TimeoutAPIError
|
||||
cdef object SocketAPIError
|
||||
cdef object InvalidAuthAPIError
|
||||
cdef object SocketClosedAPIError
|
||||
|
||||
cdef object astuple
|
||||
|
||||
|
@ -57,8 +60,8 @@ cdef object CONNECTION_STATE_CLOSED
|
|||
|
||||
cdef object make_hello_request
|
||||
|
||||
cpdef handle_timeout(object fut)
|
||||
cpdef handle_complex_message(
|
||||
cpdef void handle_timeout(object fut)
|
||||
cpdef void handle_complex_message(
|
||||
object fut,
|
||||
list responses,
|
||||
object do_append,
|
||||
|
@ -130,7 +133,7 @@ cdef class APIConnection:
|
|||
|
||||
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)
|
||||
cdef void _add_message_callback_without_remove(self, object on_message, tuple msg_types)
|
||||
|
|
|
@ -19,6 +19,7 @@ from google.protobuf import message
|
|||
|
||||
import aioesphomeapi.host_resolver as hr
|
||||
|
||||
from ._frame_helper.base import WRITE_EXCEPTIONS
|
||||
from ._frame_helper.noise import APINoiseFrameHelper
|
||||
from ._frame_helper.plain_text import APIPlaintextFrameHelper
|
||||
from .api_pb2 import ( # type: ignore
|
||||
|
@ -46,6 +47,7 @@ from .core import (
|
|||
ReadFailedAPIError,
|
||||
ResolveAPIError,
|
||||
SocketAPIError,
|
||||
SocketClosedAPIError,
|
||||
TimeoutAPIError,
|
||||
UnhandledAPIConnectionError,
|
||||
)
|
||||
|
@ -667,12 +669,16 @@ class APIConnection:
|
|||
|
||||
try:
|
||||
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
|
||||
# are in anymore and we have to close the connection
|
||||
_LOGGER.info("%s: Error writing packets: %s", self.log_name, err)
|
||||
self.report_fatal_error(err)
|
||||
raise
|
||||
wrapped_err = SocketClosedAPIError(
|
||||
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(
|
||||
self, on_message: Callable[[Any], None], msg_types: tuple[type[Any], ...]
|
||||
|
|
Loading…
Reference in New Issue