You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
Create a single timer that triggers every keepalive / 3 seconds
Every time a packet is received we shift the timer
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
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
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
In order to improve the actual implementation my idea is:
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 ashift
anddestroy
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?
The text was updated successfully, but these errors were encountered: