Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add simple API support for app MQTT pub retries #2691

Merged
merged 1 commit into from
Apr 16, 2024
Merged

add simple API support for app MQTT pub retries #2691

merged 1 commit into from
Apr 16, 2024

Conversation

scaprile
Copy link
Collaborator

@scaprile scaprile commented Apr 5, 2024

#2689

  • Should work as long as the id counter does not rollback (65536 messages sent while waiting to retransmit)
  • Does not break current API

test/unit_test.c Outdated
@@ -589,12 +593,14 @@ static void test_mqtt_ver(uint8_t mqtt_version) {
retries = 0; // don't do retries for test speed
do { // retry on failure after an expected timeout
mg_mqtt_pub(c, &opts);
opts.id = c->mgr->mqtt_id; // save id for possible resend
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to keep it in opts?
can't we just do this?

uint16_t id = c->mgr->mqtt_id;

Copy link
Collaborator Author

@scaprile scaprile Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mg_mqtt_pub() needs a way to know whether to:

  • send a new id
  • send a specific id and set the DUP flag

I modified it to make the decision based on the value of opts.id. If it is 0, it will create a new id. If it is !=0, it will use that id and set the DUP flag. The caller is responsible for keeping and clearing the id (and the message text for retransmission)

Copy link
Member

@cpq cpq Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so that opts.id is used as a signal to set the flags, I see..
I feel a bit uncomfortable about it, seems too tricky..
What if we expose flags argument to the API ? is it used anywhere else but for this retransmission?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The retransmission operation requires using the very same id of the former message, and, setting the DUP flag.
We need a way to do a retransmission instead of plain publish.

Using a new function was my first choice, that was discarded.

We can break the mg_mqtt_pub() signature by adding an extra argument, or two arguments.

  • 1 argument: address of id holder, pub stores the id there and there's no need to hold it in options nor to pick it up from the manager. Hidden logic: we set the DUP flag for the user. Seems obvious to me, as one implies the other. Cons: the user has to provide a clean id holder or a NULL pointer if don't care (to simplify usage in usual conditions).
  • 2 arguments: same as above, but the user sets the DUP flag. Cons: the user has to actually do know about this even if they just want to send QoS0 or don't care about retransmissions. Seems more complicated to me, for the same reason. It is quite prone to user errors

For the reasons above, I though hiding that "lovely" id in options was simpler and didn't break the current API
If we are going to break the API, then

QoS0+ with no interest in retransmissions:
mg_mqtt_pub(..., NULL);
QoS1+ with retransmission capabilities:
uint16_t id=0;
mg_mqtt_pub(..., &id);  // regular send, fills 'id'
mg_mqtt_pub(..., &id);  // retransmission, sets DUP flag and uses 'id'

id = 0 is invalid in the protocol, so it is a valid sign that we are sending a brand new message.

Copy link
Collaborator Author

@scaprile scaprile Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, our customer opening the issue likes this hidden logic #2689 (comment)

@scaprile scaprile self-assigned this Apr 11, 2024
@scaprile scaprile force-pushed the mqtt12 branch 2 times, most recently from 34682dc to 27e5b37 Compare April 16, 2024 22:11
@scaprile scaprile merged commit 9cccb98 into master Apr 16, 2024
46 checks passed
@scaprile scaprile deleted the mqtt12 branch April 16, 2024 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants