Skip to content

Commit

Permalink
Merge pull request #321 from HarryEMartland/feat-restart-event-reques…
Browse files Browse the repository at this point in the history
…t-after-connection-error

retry on other error codes and sets max retry delay
  • Loading branch information
agsh authored May 19, 2024
2 parents 7903189 + cfe19ca commit f1823d2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module.exports = function(Cam) {

const linerase = require('./utils').linerase;
const parseSOAPString = require('./utils').parseSOAPString;
const retryErrorCodes = ['ECONNREFUSED','ECONNRESET','ETIMEDOUT', 'ENETUNREACH'];
const maxEventReconnectMs = 2 * 60 * 1000;


/**
Expand Down Expand Up @@ -350,10 +352,11 @@ module.exports = function(Cam) {
this.createPullPointSubscription(function(error) {
if (!error) {
this._eventPull();
} else if (typeof error === 'object' && error.code === 'ECONNRESET') {
} else if (typeof error === 'object' && retryErrorCodes.includes( error.code)) {
// connection reset on creation - restart Event loop for pullMessages request
this._restartEventRequest();
}
console.log("Error: " + error.code);
}.bind(this));
} else {
this._eventPull();
Expand All @@ -376,6 +379,7 @@ module.exports = function(Cam) {
}, function(err, data, xml) {
if (!err) {
if (data.notificationMessage) {
this._eventReconnectms = 0;
if (!Array.isArray(data.notificationMessage)) {
data.notificationMessage = [data.notificationMessage];
}
Expand Down Expand Up @@ -424,11 +428,13 @@ module.exports = function(Cam) {
*/
Cam.prototype._restartEventRequest = function() {
// TODO maybe stop trying to connect after some time
if (!this._eventReconnectms || this._eventReconnectms > 2 * 60 * 1000) {
if (!this._eventReconnectms) {
this._eventReconnectms = 10;
}
setTimeout(this._eventRequest.bind(this), this._eventReconnectms);
this._eventReconnectms = 1.111 * this._eventReconnectms;
if (this._eventReconnectms < maxEventReconnectMs) {
this._eventReconnectms = 1.111 * this._eventReconnectms;
}
};

/**
Expand Down

0 comments on commit f1823d2

Please sign in to comment.