Skip to content

Commit

Permalink
Retry delay
Browse files Browse the repository at this point in the history
  • Loading branch information
rokanost committed Oct 19, 2020
1 parent f549638 commit 33a8525
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions lib/LiveMysql.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* mysql-live-select, MIT License [email protected], [email protected]
lib/LiveMysql.js - Main class */
lib/LiveMysql.js - Main class */
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var _ = require('lodash');
Expand All @@ -9,14 +9,21 @@ var EJSON = require('ejson');

// Maximum duration to wait for Zongji to initialize before timeout error (ms)
var ZONGJI_INIT_TIMEOUT = 10000;
var ZONGJI_RETRY_TIMEOUT = 4000;
// First retry (no delay) and then incremented by 2s
let ZONGJI_RETRY_DELAY = 2000;
let retryCount = 0;
const resetRetryCount = () => {
retryCount = 0;
}

function zongjiManager(dsn, options, onBinlog) {
var newInst = new ZongJi(dsn);
newInst.on('error', function(reason) {
console.log("ZongJi error:");
console.log(reason);
newInst.removeListener('binlog', onBinlog);
newInst.removeListener('ready', resetRetryCount);
console.log("Retry in (ms): ", ZONGJI_RETRY_DELAY * retryCount)
setTimeout(function() {
// If multiple errors happened, a new instance may have already been created
if(!('child' in newInst)) {
Expand All @@ -29,13 +36,15 @@ function zongjiManager(dsn, options, onBinlog) {
} else {
console.log("Resuming binlog at end");
}

newInst.child = zongjiManager(dsn, Object.assign({}, options, resumeoptions), onBinlog);
newInst.emit('child', newInst.child, reason);
newInst.child.on('child', child => newInst.emit('child', child, reason));
// Inc. retry count
retryCount += 1;
}
}, ZONGJI_RETRY_TIMEOUT);
}, ZONGJI_RETRY_DELAY * retryCount);
});
newInst.on('ready', resetRetryCount);
newInst.on('binlog', onBinlog);
newInst.start(options);
return newInst;
Expand All @@ -62,9 +71,9 @@ function LiveMysql(settings, callback) {
const binlogSettingsList = ['serverId', 'minInterval', 'checkConditionWhenQueued'];

binlogSettingsList.forEach(binlogSetting => {
if (binlogSetting in settings) {
binlogSettings[binlogSetting] = settings[binlogSetting];
delete settings[binlogSetting];
if (binlogSetting in settings) {
binlogSettings[binlogSetting] = settings[binlogSetting];
delete settings[binlogSetting];
}
});

Expand All @@ -75,23 +84,23 @@ function LiveMysql(settings, callback) {
self.poolpromise = self.pool.promise();
self.execute = self.pool.execute.bind(self.pool);

self.endDbOrPool = function() {
self.pool.end();
};
self.endDbOrPool = function() {
self.pool.end();
};

initialConnect = process.nextTick;
initialConnect = process.nextTick;
}
else
{
self.db = mysql.createConnection(settings);
self.dbpromise = self.db.promise();
self.execute = self.db.execute.bind(self.db);

self.endDbOrPool = function() {
self.db.destroy();
};
self.endDbOrPool = function() {
self.db.destroy();
};

initialConnect = self.db.connect.bind(self.db);
initialConnect = self.db.connect.bind(self.db);
}

self.settings = settings;
Expand Down Expand Up @@ -134,13 +143,13 @@ function LiveMysql(settings, callback) {
});
});

var newest = zongji;
var newest = zongji;

zongji.on('child', function(child, reason) {
//Stop old ZongJi instance and update reference
newest.stop();
newest = child;
});
zongji.on('child', function(child, reason) {
//Stop old ZongJi instance and update reference
newest.stop();
newest = child;
});

// Wait for Zongji to be ready before executing callback
var zongjiInitTime = Date.now();
Expand Down

0 comments on commit 33a8525

Please sign in to comment.