ee-db-cluster
Advanced tools
Comparing version 0.1.12 to 0.1.13
@@ -61,2 +61,8 @@ !function(){ | ||
this._createQueue('readwrite'); | ||
/*setInterval(function(){ | ||
log.info('readonly length: %s ...', this.connections.readonly.length); | ||
log.info('readwrite length: %s ...', this.connections.readonly.length); | ||
log.info('writeonly length: %s ...', this.connections.writeonly.length); | ||
}.bind(this), 1000);*/ | ||
} | ||
@@ -94,4 +100,4 @@ | ||
this.queue[id] = new Queue({ | ||
ttl: 600000 | ||
, max: 100000 | ||
ttl: 10000 // don't wait longer than 10 seconds before aborting a query | ||
, max: 100000 // don't queue more than 10k queries | ||
}); | ||
@@ -140,3 +146,3 @@ | ||
// remove connection from pool when closed | ||
connection.on('end', function(){ | ||
connection.once('end', function() { | ||
var index = this.connections[mode].indexOf(connection); | ||
@@ -150,4 +156,4 @@ if (index >= 0) this.connections[mode].splice(index, 1); | ||
// add idling connections to pool of available connections | ||
connection.on('idle', function(){ | ||
if(dev) log('got idling «'+mode+'» connection ...'); | ||
connection.on('idle', function() { | ||
if (dev) log('got idling «'+mode+'» connection ...'); | ||
this.connections[mode].push(connection); | ||
@@ -207,7 +213,15 @@ this._execute(); | ||
, query: function(query, callback){ | ||
var args = arguments; | ||
var args = [] | ||
, i = arguments.length | ||
, cb; | ||
while(i--) { | ||
if (typeof arguments[i] === 'function') cb = arguments[i]; | ||
args.unshift(arguments[i]); | ||
} | ||
this._getConnection(function(err, connection){ | ||
if (err) arg(args, 'function')(err); | ||
else connection.query.apply(connection, Array.prototype.slice.call(args)); | ||
if (err) cb(err); | ||
else connection.query.apply(connection, args); | ||
}.bind(this)); | ||
@@ -226,5 +240,11 @@ } | ||
, getConnection: function(){ | ||
var callback = arg(arguments, 'function') | ||
, readonly = arg(arguments, 'boolean', true); | ||
var readonly = true | ||
, i = arguments.length | ||
, callback; | ||
while(i--) { | ||
if (typeof arguments[i] === 'function') callback = arguments[i]; | ||
else if (typeof arguments[i] === 'boolean') readonly = arguments[i]; | ||
} | ||
// queue request | ||
@@ -243,6 +263,11 @@ this._getConnection(readonly, function(err, connection){ | ||
, _getConnection: function(){ | ||
var callback = arg(arguments, 'function') | ||
, readonly = arg(arguments, 'boolean', true) | ||
, mode = readonly ? 'readonly' : 'readwrite'; | ||
var mode = 'readonly' | ||
, i = arguments.length | ||
, callback; | ||
while(i--) { | ||
if (typeof arguments[i] === 'function') callback = arguments[i]; | ||
else if (typeof arguments[i] === 'boolean' && arguments[i] === false) mode = 'readwrite'; | ||
} | ||
// queue request | ||
@@ -249,0 +274,0 @@ if (!this.queue[mode].add(callback)) callback(new Error('Failed to add the connection request to the queue, overflow!').setName('OverflowException')); |
@@ -34,3 +34,3 @@ !function(){ | ||
// prefetch in % (e.g. 10 = 10%, if you hav a max of 50 connections, there shoudl always be 5 idling connections) | ||
, _prefetch: 10 | ||
, _prefetchPercent: 10 | ||
@@ -61,2 +61,3 @@ // time between two failed connection attempts | ||
// connections have an unique id, used for debugging | ||
, __connectionId: 0 | ||
@@ -68,3 +69,6 @@ , get _connectionId() { | ||
// number of ms we must wait until we can attempt to create a new connection | ||
, _throttleTimeout: null | ||
/** | ||
@@ -86,3 +90,7 @@ * class constructor | ||
// create connection event handler | ||
/*setInterval(function(){ | ||
log.highlight('count: '+this.count+', idle: '+this.idle+', creating: '+this._creatingCount+', maxConnections: '+this.maxConnections+', idlePercent: '+this.idlePercent+'%, prefetch: '+this._prefetchPercent+'%'); | ||
}.bind(this), 1000);*/ | ||
// fill the pool | ||
this._createConnection(); | ||
@@ -101,3 +109,3 @@ } | ||
/** | ||
* the _setIdle method decreases the idle cpunter and asks for more conenctions | ||
* the _setIdle method decreases the idle counter and asks for more conenctions | ||
*/ | ||
@@ -120,8 +128,8 @@ , _setBusy: function() { | ||
// no throtling (1000 msec)? | ||
if (this._trotthleValue === 10){ | ||
if(dev) log('count: '+this.count+', idle: '+this.idle+', creating: '+this._creatingCount+', maxConnections: '+this.maxConnections+', idlePercent: '+this.idlePercent+'%, prefetch: '+this._prefetch+'%'); | ||
// no throtling? | ||
if (this._throttleTimeout === null){ | ||
if(dev) log('count: '+this.count+', idle: '+this.idle+', creating: '+this._creatingCount+', maxConnections: '+this.maxConnections+', idlePercent: '+this.idlePercent+'%, prefetch: '+this._prefetchPercent+'%'); | ||
// dont make too many connections | ||
if (this.count < this.maxConnections && this.idlePercent < this._prefetch){ | ||
if (this.count < this.maxConnections && this.idlePercent < this._prefetchPercent){ | ||
this._executeCreateConnection(); | ||
@@ -131,5 +139,10 @@ } | ||
else { | ||
if(dev) log('throtthling connection creation «'+this._trotthleValue+'» msec ...'); | ||
if(dev) log('throtthling connection creation «'+this._throttleTimeout+'» msec ...'); | ||
// throtle connection request, dont attack the server when errors occur | ||
setTimeout(this._executeCreateConnection.bind(this), this._trotthleValue); | ||
setTimeout(function() { | ||
// dont make too many connection | ||
if (this.count < this.maxConnections && this.idlePercent < this._prefetchPercent){ | ||
this._executeCreateConnection(); | ||
} | ||
}.bind(this), this._throttleTimeout); | ||
} | ||
@@ -142,3 +155,3 @@ } | ||
*/ | ||
, _executeCreateConnection: function(){ | ||
, _executeCreateConnection: function() { | ||
var connection = new this.driver(this.options, this._connectionId); | ||
@@ -148,3 +161,3 @@ | ||
connection.on('load', function(err){ | ||
connection.on('load', function(err) { | ||
this._creatingCount--; | ||
@@ -159,11 +172,8 @@ | ||
if(dev) log('connection created ...'); | ||
this._trotthleValue = 10; | ||
this._throttleTimeout = null; | ||
this._connections.push(connection); | ||
connection.on('end', function(){ | ||
connection.once('end', function(){ | ||
var index = this._connections.indexOf(connection); | ||
if (index >= 0) this._connections.splice(index, 1); | ||
//log.wtf('closing connection, index: '+index); | ||
// decrdease idle count | ||
//this._setBusy(); | ||
@@ -185,4 +195,5 @@ if(dev) log('connection has ended, removed connection at index «'+index+'»...'); | ||
// make more | ||
setTimeout(this._createConnection.bind(this), 10); | ||
// make more conenctions, the _createConnection method will abort | ||
// this if limits / requirements are reached | ||
process.nextTick(this._createConnection.bind(this)); | ||
} | ||
@@ -195,6 +206,6 @@ | ||
, _throttle: function(){ | ||
this._trotthleValue *= 2; | ||
if (this._trotthleValue > 30000) this._trotthleValue = 30000; | ||
this._throttleTimeout = this._throttleTimeout === null ? 10 : this._throttleTimeout * 1.5; | ||
if (this._throttleTimeout > 30000) this._throttleTimeout = 30000; | ||
} | ||
}); | ||
}(); |
{ | ||
"name" : "ee-db-cluster" | ||
, "description" : "db cluster implementation for ee-orm" | ||
, "version" : "0.1.12" | ||
, "version" : "0.1.13" | ||
, "homepage" : "https://github.com/eventEmitter/ee-db-cluster" | ||
@@ -6,0 +6,0 @@ , "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19565
502