Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ee-db-cluster

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ee-db-cluster - npm Package Compare versions

Comparing version 0.1.13 to 0.1.15

97

lib/Cluster.js

@@ -1,2 +0,2 @@

!function(){
!function() {

@@ -31,20 +31,3 @@ var Class = require('ee-class')

// loaded cluster nodes
, nodes: {
readonly: []
, readwrite: []
, writeonly: []
}
// connection pool
, connections: {
readonly: []
, readwrite: []
, writeonly: []
}
// request queue
, queue: {}
/**

@@ -55,15 +38,33 @@ * class constructor

*/
, init: function(options){
, init: function(options) {
// map of all connected nodes
this.nodes = {
readonly: []
, readwrite: []
, writeonly: []
};
// map of all connections
this.connections = {
readonly: []
, readwrite: []
, writeonly: []
};
// request queues
this.queue = {};
// mysql / postgres ?
this.type = options.type;
// writeonly nodes?
this.allowReadsOnWritabelNodes = !!options.allowReadsOnWritabelNodes;
// load the requested driver
this._loadDriver();
// create queue infrastructure
this._createQueue('readonly');
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);*/
}

@@ -76,7 +77,7 @@

*/
, _loadDriver: function(){
, _loadDriver: function() {
var type = this.type;
// valid node type?
if (this.drivers[type]){
if (this.drivers[type]) {
// load driver

@@ -99,14 +100,14 @@ try {

*/
, _createQueue: function(id){
, _createQueue: function(id) {
// create connection queues
this.queue[id] = new Queue({
ttl: 10000 // don't wait longer than 10 seconds before aborting a query
, max: 100000 // don't queue more than 10k queries
, max: 10000 // don't queue more than 10k queries
});
this.queue[id].on('timeout', function(callback){
callback(new Error('Failed to get a connection!').setName('TimeoutException'));
this.queue[id].on('timeout', function(callback) {
callback(new Error('Failed to get a connection!'));
}.bind(this));
this.queue[id].on('error', function(){
this.queue[id].on('error', function() {
log.warn('DB connection queue is overflowing ...');

@@ -125,3 +126,3 @@ }.bind(this));

*/
, addNode: function(){
, addNode: function() {
var options = arg(arguments, 'object', {})

@@ -136,3 +137,3 @@ , mode = arg(arguments, 'string', options.mode || 'readonly')

// remove ended nodes
node.on('end', function(){
node.on('end', function() {
var index = this.nodes[mode].indexOf(node);

@@ -144,3 +145,3 @@ if (index >= 0) this.nodes[mode].splice(index, 1);

// emit connection
node.on('connection', function(connection){
node.on('connection', function(connection) {
if(dev) log('got free «'+mode+'» connection ...');

@@ -181,6 +182,6 @@

*/
, _execute: function(){
, _execute: function() {
// readonly stuff
if (this.queue.readonly.length){
if (this.queue.readonly.length) {
if (this.connections.readonly.length) {

@@ -197,3 +198,3 @@ this.queue.readonly.get()(null, this.connections.readonly.shift());

// writing stuff
if (this.queue.readwrite.length){
if (this.queue.readwrite.length) {
if (this.connections.writeonly.length) {

@@ -203,3 +204,3 @@ this.queue.writeonly.get()(null, this.connections.writeonly.shift());

}
else if (this.connections.readwrite.length){
else if (this.connections.readwrite.length) {
this.queue.readwrite.get()(null, this.connections.readwrite.shift());

@@ -218,3 +219,3 @@ if(dev) log('returned «readwrite» connection for a writing query ...');

*/
, query: function(query, callback){
, query: function() {
var args = []

@@ -230,3 +231,3 @@ , i = arguments.length

this._getConnection(function(err, connection){
this._getConnection(function(err, connection) {
if (err) cb(err);

@@ -246,3 +247,3 @@ else connection.query.apply(connection, args);

*/
, getConnection: function(){
, getConnection: function() {
var readonly = true

@@ -258,3 +259,3 @@ , i = arguments.length

// queue request
this._getConnection(readonly, function(err, connection){
this._getConnection(readonly, function(err, connection) {
if (err) callback(err);

@@ -270,3 +271,3 @@ else {

, _getConnection: function(){
, _getConnection: function() {
var mode = 'readonly'

@@ -297,4 +298,4 @@ , i = arguments.length

*/
, describe: function(databases, callback){
this.getConnection(function(err, connection){
, describe: function(databases, callback) {
this.getConnection(function(err, connection) {
if (err) callback(err);

@@ -312,4 +313,4 @@ else connection.describe(databases, callback);

*/
, createTransaction: function(callback){
this.getConnection(false, function(err, connection){
, createTransaction: function(callback) {
this.getConnection(false, function(err, connection) {
if (err) callback(err);

@@ -316,0 +317,0 @@ else {

@@ -16,3 +16,3 @@ !function(){

module.exports = new Class({
inherits: EventEmitter
inherits: EventEmitter

@@ -22,5 +22,2 @@ // mode of operation

// all connections
, _connections: []
// max connections

@@ -42,28 +39,29 @@ , _maxConnections: 50

// max connections
, get maxConnections() {
// max connections on the node
, maxConnections:{get: function() {
return this._maxConnections;
}
}}
// number of created conenctions
, get count() {
, count:{get: function() {
return this._connections.length + this._creatingCount;
}
}}
// number of idle connections
, get idle() {
, idle: {get: function() {
return this._idle;
}
}}
// return the percentage of idle connections
, get idlePercent() {
, idlePercent: {get: function() {
return Math.round(((this._creatingCount + this._idle)/this.maxConnections)*100);
}
}}
// connections have an unique id, used for debugging
, __connectionId: 0
, get _connectionId() {
, _connectionId: {get: function() {
if (this.__connectionId >= 9e15) this.__connectionId = 0;
return ++this.__connectionId;
}
}}

@@ -84,3 +82,9 @@ // number of ms we must wait until we can attempt to create a new connection

// connections pool
this._connections = [];
// node mode
this.mode = mode;
// driver
this.driver = driver;

@@ -91,6 +95,2 @@ this.options = options;

/*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

@@ -133,3 +133,3 @@ this._createConnection();

// dont make too many connections
if (this.count < this.maxConnections && this.idlePercent < this._prefetchPercent){
if (this.count < this.maxConnections && this.idlePercent < this._prefetchPercent) {
this._executeCreateConnection();

@@ -143,3 +143,3 @@ }

// dont make too many connection
if (this.count < this.maxConnections && this.idlePercent < this._prefetchPercent){
if (this.count < this.maxConnections && this.idlePercent < this._prefetchPercent) {
this._executeCreateConnection();

@@ -146,0 +146,0 @@ }

{
"name" : "ee-db-cluster"
, "description" : "db cluster implementation for ee-orm"
, "version" : "0.1.13"
, "version" : "0.1.15"
, "homepage" : "https://github.com/eventEmitter/ee-db-cluster"
, "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)"
, "licence" : "mit"
, "os" : "!win"
, "repository": {

@@ -20,4 +19,4 @@ "url" : "https://github.com/eventEmitter/ee-db-cluster.git"

, "dependencies": {
"ee-class" : "0.4.x"
, "ee-event-emitter" : "0.1.x"
"ee-class" : "1.0.x"
, "ee-event-emitter" : "0.2.x"
, "ee-types" : "0.1.x"

@@ -32,3 +31,2 @@ , "ee-log" : "0.2.x"

, "ee-travis" : "0.1.x"
, "ee-mysql-connection" : "0.1.x"
}

@@ -35,0 +33,0 @@ , "optionalDependencies": {}

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc