dl
Version:
DreamLab Libs
109 lines (87 loc) • 4.25 kB
JavaScript
var Class = require("core").Class;
var ConnectionPool = require("core").http.ConnectionPool;
var CredentialsManager = require("../credentials/Manager.js").CredentialsManager;
var crypto = require("core").crypto;
var CredentialConnectionPool = function () {
this.Extends = ConnectionPool;
this.initialize = function (credentialKey, appName) {
this.parent({});
this._cm = null;
this._throttleDisconnects = false;
this._credentialKey;
this._concurrentDisconnects = 0;
this._maxConcurrentDisconnects = 50;
this._concurrentDisconnectsRatio = 0.1;
this._options = null;
var self = this;
setInterval(function () {
console.info('CONNECTION POOL: listeners waiting: ' + self._callbacks.length + ' sockets available: ' + self._sockets.length );
}, 30000);
this._credentialKey = credentialKey;
this._cm = CredentialsManager.factory(appName);
this._cm.addEventListener(CredentialsManager.Event.LOADED, credentialKey, function (data) {
var index = Math.round(Math.random() * (data.data.hosts.length - 1));
self._options = {
host: data.data.hosts[index].host,
port: data.data.hosts[index].port,
connections: data.data.connectionPool || 10
};
console.log(' - CredentialConnectionPool: Received Credentials', JSON.stringify(self._options));
if (self._sockets.length == 0) {
self._connect();
} else {
self._reconnect();
}
}).addEventListener(CredentialsManager.Event.ERROR, credentialKey, function (data) {
console.error('CREDENTIAL ERROR: ' + credentialKey + '. Error while receiving credential data for CredentialConnectionPool', JSON.stringify(data.data));
setTimeout(function () {
console.info('CREDENTIAL INFO: getting credential -> ' + credentialKey);
self._cm.renewCredential(credentialKey);
}, 500);
});
this._cm.getCredential(credentialKey);
};
this._connect = function () {
console.log(' - CredentialConnectionPool: Connecting');
this.parent();
};
this._reconnect = function () {
console.log(' - CredentialConnectionPool: Reconnecting');
for (var i = 0, l = this._sockets.length; i < l; i++) {
//console.log('Destroying and reconnecting to socket:', i, this._options);
this._sockets[i]._socket.destroy();
//to sie dzieje w zdarzeniu close ktore jest wywolywane przy .destroy()
this._sockets[i]._socket.connect(this._options.port, this._options.host);
}
this._throttleDisconnects = false;
};
this._refineSocketHandlers = function (socket) {
this.parent(socket);
var self = this;
socket.on('connect', function () {
socket._disconnects = 0;
self._concurrentDisconnects--;
});
if (socket.__onClose == undefined) {
socket.__onClose = socket._onClose;
}
socket._onClose = function (sck) {
(socket._disconnects) ? socket._disconnects++ : socket._disconnects = 1;
self._concurrentDisconnects++;
if (!self._throttleDisconnects) {
console.log(' - CredentialConnectionPool: Disconnects: ' + socket._disconnects + ' | Concurrent Disconnects: ' + self._concurrentDisconnects);
if (socket._disconnects >= 5 || self._concurrentDisconnects >= self._maxConcurrentDisconnects) {
self.
_throttleDisconnects = true;
setTimeout(function () {
self._cm.renewCredential(self._credentialKey);
}, 500);
} else {
socket.__onClose(sck);
}
}
};
};
};
CredentialConnectionPool = new Class(new CredentialConnectionPool());
exports.CredentialConnectionPool = CredentialConnectionPool;