-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: keepalive manager #1865
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
Merged
Merged
feat: keepalive manager #1865
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9074fd3
fix: keepalive management
robertsLando 2f04e6c
fix: skip shift on 'publish' packets
robertsLando cb815c9
Merge branch 'main' of https://github.com/mqttjs/MQTT.js into feat-ke…
robertsLando 69ab395
fix: better keepalive checks
robertsLando 06e2290
fix: refactor methods
robertsLando 1d9dc20
fix: example
robertsLando d5da083
refactor: rename method
robertsLando d9491ed
fix: keepalive tests
robertsLando 36bfb4e
fix: remove .only
robertsLando 41ca373
fix: connack
robertsLando 9a50256
fix: log mock
robertsLando b6d19a5
Merge branch 'main' into feat-keepalive
robertsLando 8d32645
fix: do not allow setting 0 as keepalive in manager
robertsLando 9fa3769
fix: remove useless rescheduling on connack
robertsLando 37a5271
fix: flaky test
robertsLando 1cd488a
fix: add math.ceil to keepalive interval every
robertsLando File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import type MqttClient from './client' | ||
import getTimer, { type Timer } from './get-timer' | ||
import type { TimerVariant } from './shared' | ||
|
||
export default class KeepaliveManager { | ||
private _keepalive: number | ||
|
||
private timerId: number | ||
|
||
private timer: Timer | ||
|
||
private destroyed = false | ||
|
||
private counter: number | ||
|
||
private client: MqttClient | ||
|
||
private _keepaliveTimeoutTimestamp: number | ||
|
||
/** Timestamp of next keepalive timeout */ | ||
get keepaliveTimeoutTimestamp() { | ||
return this._keepaliveTimeoutTimestamp | ||
} | ||
|
||
set keepalive(value: number) { | ||
if ( | ||
// eslint-disable-next-line no-restricted-globals | ||
isNaN(value) || | ||
value < 0 || | ||
value * 1000 > 2147483647 | ||
robertsLando marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
throw new Error( | ||
`Keepalive value must be a number between 0 and 2147483647. Provided value is ${this._keepalive}`, | ||
) | ||
} | ||
|
||
this._keepalive = value * 1000 | ||
} | ||
|
||
get keepalive() { | ||
return this._keepalive | ||
} | ||
|
||
constructor(client: MqttClient, variant: TimerVariant) { | ||
this.keepalive = client.options.keepalive | ||
this.client = client | ||
this.timer = getTimer(variant) | ||
this.reschedule() | ||
} | ||
|
||
private clear() { | ||
if (this.timerId) { | ||
this.timer.clear(this.timerId) | ||
this.timerId = null | ||
} | ||
} | ||
|
||
destroy() { | ||
this.clear() | ||
this.destroyed = true | ||
} | ||
|
||
reschedule() { | ||
if (this.destroyed) { | ||
return | ||
} | ||
|
||
this.clear() | ||
this.counter = 0 | ||
// https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Figure_3.5_Keep | ||
this._keepaliveTimeoutTimestamp = Date.now() + this._keepalive * 1.5 | ||
robertsLando marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
this.timerId = this.timer.set(() => { | ||
// this should never happen, but just in case | ||
if (this.destroyed) { | ||
return | ||
} | ||
|
||
this.counter += 1 | ||
|
||
// after keepalive seconds, send a pingreq | ||
if (this.counter === 2) { | ||
this.client.sendPing() | ||
} else if (this.counter > 2) { | ||
this.client.onKeepaliveTimeout() | ||
} | ||
}, this._keepalive / 2) | ||
robertsLando marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.