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

[feat] Keepalive improvements #1859

Closed
robertsLando opened this issue May 8, 2024 · 0 comments · Fixed by #1865
Closed

[feat] Keepalive improvements #1859

robertsLando opened this issue May 8, 2024 · 0 comments · Fixed by #1865

Comments

@robertsLando
Copy link
Member

robertsLando commented May 8, 2024

Actually keepalive works this way (PingTimer):

A timer is setup, when timer is elapsed it triggers a function to check if last received control message was more then our keepalive time, if ok it schedule a new check and sends a pingreq otherwise closes the connection and triggers a reconnect. Every time we receive a packet we shift the timer.

Specs say: https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Figure_3.5_Keep

If the Keep Alive value is non-zero and the Server does not receive a Control Packet from the Client within one and a half times the Keep Alive time period, it MUST disconnect the Network Connection to the Client as if the network had failed [MQTT-3.1.2-24].

In order to improve the actual implementation my idea is:

  1. Create a single timer that triggers every keepalive / 3 seconds
  2. Every time a packet is received we shift the timer
  3. Every time the timer triggers we increese a counter, when it reaches value 3 it triggers the close and reconnect. When the counter reaches value 2 we send a pingreq packet
  4. The counter is resetted on every shift

In order to do this I would pass the client instance to the PingTimer class (that I would rename in KeepaliveManager) and it should just expose a shift and destroy method and handle above logic itself by calling client methods like checkPing (instead of providing the checkping function in constructor).

In this way we would be sure that the connection is closed exaclty after 1.5*keepalive seconds without any packet

NOTE: Remeber to check timer is never greather then 32 bit or less then 0

cc @mcollina opinions/suggestions on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant