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

MQTT reconnect is slow (IDFGH-508) #2850

Closed
kanstrup opened this issue Dec 19, 2018 · 0 comments
Closed

MQTT reconnect is slow (IDFGH-508) #2850

kanstrup opened this issue Dec 19, 2018 · 0 comments

Comments

@kanstrup
Copy link

MQTT reconnect is slow

Environment

  • Development Kit: [none]
  • Module or chip used: [ESP32]
  • IDF version (run git describe --tags to find it): v3.3-beta1-64-gd8b625a
  • Build System: [Make]
  • Compiler version (run xtensa-esp32-elf-gcc --version to find it): 1.22.0-80-g6c4433a
  • Operating System: [Linux]
  • Power Supply: [external 5V]

Problem Description

MQTT takes longer time than necessary to reconnect after network connection has been re-established.

esp-mqtt library is unaware of underlying network connectivity states and current auto reconnect mechanism is built around timed retry attempts every MQTT_RECONNECT_TIMEOUT_MS.

MQTT_RECONNECT_TIMEOUT_MS is set to 10 seconds per default and together with a bug that makes the reconnect delay another MQTT_RECONNECT_TIMEOUT_MS makes MQTT reconnection between 10-20 seconds slower than it has to.

As application code usually keeps track of network connectivity state exporting a new function to force reconnect would help applications that need instant reconnect.

I've opened a pull request on EspressIf/esp-mqtt repo that both export the new reconnect function and solves the above mentioned extra reconnect delay. As it's a submodule I'm not sure you are aware of it.

espressif/esp-mqtt#83

Expected Behavior

  1. Connect to wifi network
  2. Connect mqtt
  3. Wait for wifi disconnect (this disconnect can be any reason)
  4. Wait for wifi reconnect
  5. Observe mqtt instant reconnect

Actual Behavior

  1. Connect to wifi network
  2. Connect mqtt
  3. Wait for wifi disconnect (this disconnect can be any reason)
  4. Wait for wifi reconnect
  5. Notice that after wifi successfully reconnected mqtt takes another 10-20 seconds before connecting.

Steps to reproduce

  1. Apply patch 0001-MQTT-slow-reconnect-example.patch
  2. Build and flash examples/protocols/mqtt/ssl example application.
  3. Run for a while and look for these printouts:
I (5570) MQTTS_EXAMPLE: MQTT (re-)connect took 2.00 seconds
I (22360) MQTTS_EXAMPLE: MQTT (re-)connect took 15.00 seconds
I (39170) MQTTS_EXAMPLE: MQTT (re-)connect took 14.00 seconds
...

Notice the initial mqtt connect takes about 2 seconds. Reconnecting takes 10-20 seconds.

Code to reproduce this issue

examples/protocols/mqtt/ssl example application with patch 0001-MQTT-slow-reconnect-example.patch applied.

Solution

  1. Pull mqtt reconnect improvements esp-mqtt#83
  2. Apply 0002-Force-MQTT-reconnect-on-wifi-connected.patch
  3. Build and flash examples/protocols/mqtt/ssl example application
  4. Run for a while and look for these printouts:
I (5541) MQTTS_EXAMPLE: MQTT (re-)connect took 2 seconds
I (9541) MQTTS_EXAMPLE: MQTT (re-)connect took 2 seconds
I (13371) MQTTS_EXAMPLE: MQTT (re-)connect took 2 seconds
...

Notice both initial mqtt connect and reconnect takes about 2 seconds.

Other items if possible

0001-MQTT-slow-reconnect-example.patch.txt

0002-Force-MQTT-reconnect-on-wifi-connected.patch.txt

@Alvin1Zhang Alvin1Zhang changed the title MQTT reconnect is slow [TW#28001] MQTT reconnect is slow Dec 19, 2018
@projectgus projectgus changed the title [TW#28001] MQTT reconnect is slow MQTT reconnect is slow (IDFGH-508) Mar 12, 2019
@igrr igrr closed this as completed in 1465f53 Mar 27, 2019
david-cermak added a commit to espressif/esp-mqtt that referenced this issue Dec 16, 2022
…ng/receiving different data and references esp-mqtt commits to pass these tests

testing conditions:
transports (tcp, ssl, ws..)
qos (0, 1, 2)
short repeated messages (packed packets)
oversized messages (fragmented packets)
publish from a different thread

Closes espressif/esp-idf#2870 by means of including commit 815623d from esp-mqtt
Closes espressif/esp-idf#2975 by means of including commit 752953d from esp-mqtt
Closes espressif/esp-idf#2850 by means of including commits df455d2 17fd713 from esp-mqtt
egnor pushed a commit to egnor/esp-mqtt that referenced this issue Dec 23, 2022
…ng/receiving different data and references esp-mqtt commits to pass these tests

testing conditions:
transports (tcp, ssl, ws..)
qos (0, 1, 2)
short repeated messages (packed packets)
oversized messages (fragmented packets)
publish from a different thread

Closes espressif/esp-idf#2870 by means of including commit 815623d from esp-mqtt
Closes espressif/esp-idf#2975 by means of including commit 752953d from esp-mqtt
Closes espressif/esp-idf#2850 by means of including commits df455d2 17fd713 from esp-mqtt
egnor pushed a commit to egnor/esp-mqtt that referenced this issue Dec 23, 2022
…ng/receiving different data and references esp-mqtt commits to pass these tests

testing conditions:
transports (tcp, ssl, ws..)
qos (0, 1, 2)
short repeated messages (packed packets)
oversized messages (fragmented packets)
publish from a different thread

Closes espressif/esp-idf#2870 by means of including commit 815623d from esp-mqtt
Closes espressif/esp-idf#2975 by means of including commit 752953d from esp-mqtt
Closes espressif/esp-idf#2850 by means of including commits df455d2 17fd713 from esp-mqtt
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

No branches or pull requests

1 participant