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

Fix MQTT reconnection bug #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 3 additions & 19 deletions lib/bindings/mqttClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const config = require('../configService');
const context = {
op: 'IoTAgentLoRaWAN.MqttClient'
};
const sleep = (waitTimeInMs) => new Promise((resolve) => setTimeout(resolve, waitTimeInMs));
//const sleep = (waitTimeInMs) => new Promise((resolve) => setTimeout(resolve, waitTimeInMs));

/**
*MQTT client
Expand Down Expand Up @@ -63,6 +63,7 @@ class MqttClient {

this.options.username = username;
this.options.password = password;
this.options.reconnectPeriod = 1000 * 60 * 10;
this.topics = [];
this.listener = listener;
this.id = id + '_' + Math.random().toString(16).substr(2, 8);
Expand All @@ -77,9 +78,6 @@ class MqttClient {
start(callback) {
const host = this.protocol + '://' + this.host + ':' + this.port;

const retries = 5;
let retryTime = 5;
let numRetried = 0;

let connected = false;
config
Expand All @@ -106,21 +104,7 @@ class MqttClient {

this.mqttClient.on('reconnect', async function () {
config.getLogger().warn(context, 'MQTT Client reconnect: %s', this.options.clientId);
if (numRetried <= retries) {
retryTime = retryTime + 10 * numRetried;
numRetried++;
config.getLogger().debug(context, 'elapsed %s', retryTime);
await sleep(retryTime * 1000);
} else {
config
.getLogger()
.error(context, 'Too many MQTT reconnect attempts, ending client: %s', this.options.clientId);
this.end(function () {
if (callback) {
return callback();
}
});
}

});
}

Expand Down