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

Randomise reconnect timing #315

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.NODE_ENV = 'test'
4 changes: 4 additions & 0 deletions lib/agent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { IntervalJitter } = require('./IntervalJitter')
const { existsSync } = require('fs')
const { randomInt } = require('crypto')
const fs = require('fs/promises')
const path = require('path')
const httpClient = require('./http')
Expand All @@ -8,6 +9,7 @@ const Launcher = require('./launcher.js')
const { info, warn, debug } = require('./logging/log')
const utils = require('./utils.js')
const { States, isTargetState, isValidState } = require('./states')
const MQTT_CONNECT_DELAY_MAX = process.env.NODE_ENV === 'test' ? 25 : 5000

const PROJECT_FILE = 'flowforge-project.json'

Expand Down Expand Up @@ -135,6 +137,8 @@ class Agent {
}
// We have been provided a broker URL to use
this.mqttClient = mqttClient.newMQTTClient(this, this.config)
// Wait a short random delay to reduce stress on broker when large numbers of devices come on-line
await new Promise(_resolve => setTimeout(_resolve, randomInt(20, MQTT_CONNECT_DELAY_MAX)))
this.mqttClient.start()
this.mqttClient.setApplication(this.currentApplication)
this.mqttClient.setProject(this.currentProject)
Expand Down
3 changes: 2 additions & 1 deletion lib/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { IntervalJitter } = require('./IntervalJitter')
const url = require('url')
const EditorTunnel = require('./editor/tunnel')
const { getWSProxyAgent } = require('./utils')
const { randomInt } = require('crypto')

class MQTTClient {
/**
Expand Down Expand Up @@ -42,7 +43,7 @@ class MQTTClient {
clientId: config.brokerUsername,
username: config.brokerUsername,
password: config.brokerPassword,
reconnectPeriod: 15000,
reconnectPeriod: randomInt(13000, 25000),
queueQoSZero: false
}
setMQTT(this)
Expand Down