Skip to content

Concurrent use of PacketIdManager #52

@smaeul

Description

@smaeul

With the current design, allocating a packet ID requires a mutable reference to the Connection, so it is difficult to generate MQTT packets from multiple threads (or async contexts) and send them over a channel to the thread running the MQTT client event loop (especially if the Connection object is local to the MQTT client thread). Some solutions I can think of:

  1. Pass my own message struct over the channel, and generate Publish/Subscribe/Unsubscribe packets on the MQTT client thread.
  2. Pass the packet builder over the channel, and call builder.packet_id(foo).build() on the MQTT client thread.
  3. Add an option for the MQTT client to allocate and overwrite the packet ID when serializing the packet inside checked_send.
  4. Allow Connection to use an external PacketIdManager trait object, so I can wrap the PacketIdManager in a mutex and access it directly from the other threads.

Do you have any suggestions?

One reason I want to pass the pre-constructed packets over the channel, beyond having them pre-validated, is that they own the topic string/payload data, so they can be sent by value without additional boxing. But if I send my own message struct over the channel, at least the topic name will be copied again when constructing the MqttString. Would it make sense to change the builder::topic_name() methods to take an Into<MqttString> (like payloads take IntoPayload), so the channel sender can pre-create the MqttString?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions