dl
Version:
DreamLab Libs
114 lines (89 loc) • 3.54 kB
JavaScript
var PGPool = require('core').client.PGPool;
var CredentialsProvider = require('core').credentials.CredentialsProvider;
var PGCredentialPool = function (credentialProvider) {
PGPool.call(this);
this._credentialProvider = credentialProvider;
this._hosts = {};
};
PGCredentialPool.prototype = Object.create(PGPool.prototype);
PGCredentialPool.prototype._onCredentialLoaded = function (e) {
var data = e.data;
console.info('PGCredentialPool:', 'Credentials loaded', 'available hosts:', data.hosts);
var max_connections = data.max_connections || PGPool.DEFAULT_CONN_COUNT;
var staleHosts = this.mergeHosts(data.hosts);
var connectionOptions = {
user: data.user,
password: data.pass,
hosts: data.hosts,
database: data.db_name,
keepalives_idle: data.keepalives_idle,
keepalives_interval: data.keepalives_interval,
keepalives_count: data.keepalives_count
};
this.setMaxLength(max_connections);
this.setConnectionOptions(connectionOptions);
this._dropConnections(staleHosts);
this._handleConnectionCount();
};
PGCredentialPool.prototype.getConnectionOptions = function() {
var options = this._config;
var randIndex = Math.floor(Math.random() * options.hosts.length);
var host = options.hosts[randIndex].host;
var port = options.hosts[randIndex].port;
options.host = host;
options.port = port;
return options;
};
PGCredentialPool.prototype._onCredentialError = function (e) {
console.info('PGCredentialPool:', 'Error while receiving credential:', e);
};
PGCredentialPool.prototype._onCredentialTimeout = function (e) {
console.info('PGCredentialPool:', 'Timeout while receiving credential:', e);
};
PGCredentialPool.prototype.connect = function () {
var that = this;
var attachListeners = function () {
that._credentialProvider.addEventListener(CredentialsProvider.Event.LOAD, that._onCredentialLoaded, that);
that._credentialProvider.addEventListener(CredentialsProvider.Event.ERROR, that._onCredentialError, that);
that._credentialProvider.addEventListener(CredentialsProvider.Event.TIMEOUT, that._onCredentialTimeout, that);
};
this._credentialProvider.get(function (error, data) {
attachListeners();
if (!error && data) {
that._onCredentialLoaded({data: data});
} else {
that._credentialProvider.refresh();
}
});
};
PGCredentialPool.prototype._dropConnections = function (staleHosts) {
console.info('PGCredentialPool: dropping connections to:', Object.keys(staleHosts));
var host;
var clients;
for (var key in staleHosts) {
if (staleHosts.hasOwnProperty(key)) {
host = staleHosts[key];
clients = this.getCollection(function (client) {
return client.host == host.host && client.port == host.port;
});
for (var i = 0, len = clients.length; i < len; i++) {
this._removeClient(clients[i]);
}
}
}
};
PGCredentialPool.prototype.mergeHosts = function (hosts) {
var host;
var key;
var toRemove = this._hosts;
this._hosts = {};
for (var i = 0, len = hosts.length; i < len; i++) {
host = hosts[i];
key = [host.host, host.port].join(':');
delete toRemove[key];
this._hosts[key] = host;
}
return toRemove;
};
PGCredentialPool.RENEW_TIMEOUT = 1000;
exports.PGCredentialPool = PGCredentialPool;