Skip to content

Commit

Permalink
feat: speed up checking if a message needs a reply (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 9, 2022
1 parent 932053b commit d1366ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/dbus_fast/message_bus.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ cdef object MessageFlag

cdef object MESSAGE_TYPE_CALL
cdef object MESSAGE_TYPE_SIGNAL
cdef object NO_REPLY_EXPECTED_VALUE
cdef object assert_object_path_valid
cdef object assert_bus_name_valid

cdef _expects_reply(Message msg)

cdef class SendReply:

cdef object _bus
Expand Down
13 changes: 8 additions & 5 deletions src/dbus_fast/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

MESSAGE_TYPE_CALL = MessageType.METHOD_CALL
MESSAGE_TYPE_SIGNAL = MessageType.SIGNAL
NO_REPLY_EXPECTED_VALUE = MessageFlag.NO_REPLY_EXPECTED.value


def _expects_reply(msg) -> bool:
return not (msg.flags.value & NO_REPLY_EXPECTED_VALUE)


class SendReply:
Expand All @@ -43,10 +48,8 @@ def __enter__(self):
return self

def __call__(self, reply: Message) -> None:
if self._msg.flags & MessageFlag.NO_REPLY_EXPECTED:
return

self._bus.send(reply)
if _expects_reply(self._msg):
self._bus.send(reply)

def _exit(
self,
Expand Down Expand Up @@ -753,7 +756,7 @@ def reply_notify(reply: Optional[Message], err: Optional[Exception]) -> None:
self._name_owners[msg.destination] = reply.sender
callback(reply, err) # type: ignore[misc]

no_reply_expected = msg.flags & MessageFlag.NO_REPLY_EXPECTED
no_reply_expected = not _expects_reply(msg)

# Make sure the return reply handler is installed
# before sending the message to avoid a race condition
Expand Down

0 comments on commit d1366ac

Please sign in to comment.