Reduce the delay of ASYNC connection #1458
Open
+5
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolve the issue mentioned in #1430
Move the MQTT layer connect from receive thread to send thread as the TCP/Websocket connection is done in send thread. The root cause of the 2 seconds delay mentioned in issue is that, MQTT connection needs to wait two rounds of timeout (1s) polling a socket to be ready. For the first time,
Socket_getReadySocket
is polling events for first socket before second socket is connected and added to fds_read. For the second time, the second socket is connected, but there is no incoming data. By moving theMQTTAsync_connecting
to send thread, MQTT connection can start immediately after underlayer connection is established.I also removed 100ms sleep after checking
Socket_getReadySocket
. Please double check if this makes sense. Or else, each packet handling may encounter 100ms delay when no socket is ready. This is critical for applications pursuing performance.Another change is the timeout reducing from 1000ms to 10ms in the receive thread. It will make the receiving thread more responsive while has little impact on the CPU usage.
Here is the result with modified timeout in sample code. mqtt_sample.txt