Skip to content

Commit

Permalink
feat: improve performance of processing incoming messages (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Aug 21, 2023
1 parent 2b08f9f commit ce61aea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/dbus_fast/message_bus.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cdef class BaseMessageBus:
cdef public object unique_name
cdef public object _disconnected
cdef public object _user_disconnect
cdef public object _method_return_handlers
cdef public cython.dict _method_return_handlers
cdef public object _serial
cdef public cython.dict _path_exports
cdef public cython.list _user_message_handlers
Expand Down
22 changes: 10 additions & 12 deletions src/dbus_fast/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ def _call(
if not msg.serial:
msg.serial = self.next_serial()

no_reply_expected = not _expects_reply(msg)
no_reply_expected = _expects_reply(msg) is False
# Make sure the return reply handler is installed
# before sending the message to avoid a race condition
# where the reply is lost in case the backend can
Expand Down Expand Up @@ -827,6 +827,7 @@ def _check_method_return(
)

def _process_message(self, msg: _Message) -> None:
"""Process a message received from the message bus."""
handled = False
for user_handler in self._user_message_handlers:
try:
Expand All @@ -842,13 +843,9 @@ def _process_message(self, msg: _Message) -> None:
handled = True
break
else:
logging.error(
f"A message handler raised an exception: {e}.\n{traceback.format_exc()}"
)
logging.exception("A message handler raised an exception: %s", e)
except Exception as e:
logging.error(
f"A message handler raised an exception: {e}.\n{traceback.format_exc()}"
)
logging.exception("A message handler raised an exception: %s", e)
if msg.message_type is MESSAGE_TYPE_CALL:
self.send(
Message.new_error(
Expand Down Expand Up @@ -877,7 +874,7 @@ def _process_message(self, msg: _Message) -> None:
if msg.message_type is MESSAGE_TYPE_CALL:
if not handled:
handler = self._find_message_handler(msg)
if not _expects_reply(msg):
if _expects_reply(msg) is False:
if handler:
handler(msg, BLOCK_UNEXPECTED_REPLY)
else:
Expand All @@ -898,15 +895,16 @@ def _process_message(self, msg: _Message) -> None:
Message.new_error(
msg,
ErrorType.UNKNOWN_METHOD,
f'{msg.interface}.{msg.member} with signature "{msg.signature}" could not be found',
f"{msg.interface}.{msg.member} with signature "
f'"{msg.signature}" could not be found',
)
)
return

# An ERROR or a METHOD_RETURN
if msg.reply_serial in self._method_return_handlers:
return_handler = self._method_return_handlers.get(msg.reply_serial)
if return_handler is not None:
if not handled:
return_handler = self._method_return_handlers[msg.reply_serial]
return_handler(msg, None)
del self._method_return_handlers[msg.reply_serial]

Expand All @@ -927,7 +925,7 @@ def _callback_method_handler(
"""This is the callback that will be called when a method call is."""
args = msg_body_to_args(msg) if msg.unix_fds else msg.body
result = method_fn(interface, *args)
if send_reply is BLOCK_UNEXPECTED_REPLY or not _expects_reply(msg):
if send_reply is BLOCK_UNEXPECTED_REPLY or _expects_reply(msg) is False:
return
body, fds = fn_result_to_body(
result,
Expand Down

0 comments on commit ce61aea

Please sign in to comment.