Comparing version 0.0.8 to 0.0.9
var nMemcached = require( '../' ), | ||
memcached; | ||
// connect to a unknown server | ||
memcached = new nMemcached( "10.211.55.6:11211" ); | ||
memcached = new nMemcached( "10.211.55.5:11211" ); | ||
@@ -27,3 +27,3 @@ // each time a server fails | ||
}); | ||
}, 5010 ); | ||
}, 5010 ); |
@@ -1,12 +0,12 @@ | ||
var nMemcached = require( '../' ), | ||
memcached; | ||
var nMemcached = require( '../' ), | ||
memcached; | ||
// connect to our memcached server on host 10.211.55.5, port 11211 | ||
memcached = new nMemcached( "10.211.55.5:11211" ); | ||
memcached = new nMemcached( "10.211.55.5:11211", { debug: true }); | ||
memcached.set( "hello", 1, 10000, function( err, result ){ | ||
if( err ) console.error( err ); | ||
console.dir( result ); | ||
memcached.end(); // as we are 100% certain we are not going to use the connection again, we are going to end it | ||
}); | ||
if( err ) console.error( err ); | ||
console.dir( result ); | ||
memcached.end(); // as we are 100% certain we are not going to use the connection again, we are going to end it | ||
}); |
@@ -0,3 +1,5 @@ | ||
"use strict"; | ||
var EventEmitter = require('events').EventEmitter | ||
, Spawn = require('child_process').spawn | ||
, spawn = require('child_process').spawn | ||
, Utils = require('./utils'); | ||
@@ -9,58 +11,58 @@ | ||
function ping(host, callback){ | ||
var pong = Spawn('ping', [host]); | ||
pong.stdout.on('data', function(data) { | ||
function ping (host, callback) { | ||
var pong = spawn('ping', [host]); | ||
pong.stdout.on('data', function stdoutdata (data) { | ||
callback(false, data.toString().split('\n')[0].substr(14)); | ||
pong.kill(); | ||
}); | ||
pong.stderr.on('data', function(data) { | ||
pong.stderr.on('data', function stderrdata (data) { | ||
callback(data.toString().split('\n')[0].substr(14), false); | ||
pong.kill(); | ||
}); | ||
}; | ||
} | ||
function IssueLog(args){ | ||
function IssueLog (args) { | ||
this.config = args; | ||
this.messages = []; | ||
this.failed = false; | ||
this.totalRetries = 0; | ||
this.totalReconnectsAttempted = 0; | ||
this.totalReconnectsSuccess = 0; | ||
Utils.merge(this, args); | ||
EventEmitter.call(this); | ||
}; | ||
} | ||
var issues = IssueLog.prototype = new EventEmitter; | ||
issues.log = function(message){ | ||
issues.log = function log (message) { | ||
var issue = this; | ||
this.failed = true; | ||
this.messages.push(message || 'No message specified'); | ||
if (this.retries){ | ||
setTimeout(Utils.curry(issue, issue.attemptRetry), this.retry); | ||
if (this.retries) { | ||
setTimeout(issue.attemptRetry.bind(issue), this.retry); | ||
return this.emit('issue', this.details); | ||
} | ||
if (this.remove) return this.emit('remove', this.details) | ||
setTimeout(Utils.curry(issue, issue.attemptReconnect), this.reconnect); | ||
if (this.remove) return this.emit('remove', this.details); | ||
setTimeout(issue.attemptReconnect.bind(issue), this.reconnect); | ||
}; | ||
Object.defineProperty(issues, 'details', { | ||
get: function(){ | ||
get: function getDetails () { | ||
var res = {}; | ||
res.server = this.serverAddress; | ||
res.tokens = this.tokens; | ||
res.messages = this.messages; | ||
if (this.retries){ | ||
if (this.retries) { | ||
res.retries = this.retries; | ||
res.totalRetries = this.totalRetries | ||
res.totalRetries = this.totalRetries; | ||
} else { | ||
@@ -72,3 +74,3 @@ res.totalReconnectsAttempted = this.totalReconnectsAttempted; | ||
} | ||
return res; | ||
@@ -78,3 +80,3 @@ } | ||
issues.attemptRetry = function(){ | ||
issues.attemptRetry = function attemptRetry () { | ||
this.totalRetries++; | ||
@@ -85,21 +87,21 @@ this.retries--; | ||
issues.attemptReconnect = function(){ | ||
issues.attemptReconnect = function attemptReconnect () { | ||
var issue = this; | ||
this.totalReconnectsAttempted++; | ||
this.emit('reconnecting', this.details); | ||
// Ping the server | ||
ping(this.tokens[1], function(err){ | ||
ping(this.tokens[1], function pingpong (err) { | ||
// still no access to the server | ||
if (err){ | ||
this.messages.push(message || 'No message specified'); | ||
return setTimeout(Utils.curry(issue, issue.attemptReconnect), issue.reconnect); | ||
if (err) { | ||
this.messages.push(err.message || 'No message specified'); | ||
return setTimeout(issue.attemptReconnect.bind(issue), issue.reconnect); | ||
} | ||
issue.emit('reconnected', issue.details); | ||
issue.totalReconnectsSuccess++; | ||
issue.messages.length = 0; | ||
issue.failed = false; | ||
// we connected again, so we are going through the whole cycle again | ||
@@ -110,3 +112,3 @@ Utils.merge(issue, JSON.parse(JSON.stringify(issue.config))); | ||
function ConnectionManager(name, limit, constructor){ | ||
function ConnectionManager (name, limit, constructor) { | ||
this.name = name; | ||
@@ -116,53 +118,59 @@ this.total = limit; | ||
this.connections = []; | ||
}; | ||
} | ||
var Manager = ConnectionManager.prototype; | ||
Manager.allocate = function(callback){ | ||
var total | ||
, i = total = this.connections.length | ||
Manager.allocate = function allocate (callback) { | ||
var total, i | ||
, Manager = this; | ||
i = total = this.connections.length; | ||
// check for available | ||
while(i--){ | ||
if (this.isAvailable(this.connections[i])){ | ||
while (i--){ | ||
if (this.isAvailable(this.connections[i])) { | ||
return callback(false, this.connections[i]); | ||
} | ||
} | ||
// create new | ||
if (total < this.total){ | ||
if (total < this.total) { | ||
return this.connections.push(this.factory.apply(this, arguments)); | ||
} | ||
// wait untill the next event loop tick, to try again | ||
process.nextTick(function(){Manager.allocate(callback)}); | ||
process.nextTick(function next () { | ||
Manager.allocate(callback); | ||
}); | ||
}; | ||
Manager.isAvailable = function(connection){ | ||
Manager.isAvailable = function isAvailable (connection) { | ||
var readyState = connection.readyState; | ||
return (readyState == 'open' || readyState == 'writeOnly') && !(connection._writeQueue && connection._writeQueue.length); | ||
return (readyState === 'open' || readyState === 'writeOnly') | ||
&& !(connection._writeQueue && connection._writeQueue.length); | ||
}; | ||
Manager.remove = function(connection){ | ||
Manager.remove = function remove (connection) { | ||
var position = this.connections.indexOf(connection); | ||
if (position !== -1) this.connections.splice(position, 1); | ||
if (connection.readyState && connection.readyState !== 'closed' && connection.end) connection.end(); | ||
if (connection.readyState && connection.readyState !== 'closed' && connection.end) { | ||
connection.end(); | ||
} | ||
}; | ||
Manager.free = function(keep){ | ||
Manager.free = function freemymemories (keep) { | ||
var save = 0 | ||
, connection; | ||
while(this.connections.length){ | ||
while (this.connections.length) { | ||
connection = this.connections.shift(); | ||
if(save < keep && this.isAvailable(this.connection[0])){ | ||
save++ | ||
if (save < keep && this.isAvailable(this.connection[0])) { | ||
save++; | ||
continue; | ||
} | ||
this.remove(connection); | ||
} | ||
}; |
@@ -22,2 +22,8 @@ "use strict"; | ||
/** | ||
* Variable lookups | ||
*/ | ||
var curry = Utils.curry; | ||
/** | ||
* Constructs a new memcached client | ||
@@ -34,2 +40,3 @@ * | ||
, weights = {} | ||
, regular = 'localhost:11211' | ||
, key; | ||
@@ -39,6 +46,2 @@ | ||
switch (Object.prototype.toString.call(args)) { | ||
case '[object String]': | ||
servers.push(args); | ||
break; | ||
case '[object Object]': | ||
@@ -49,4 +52,8 @@ weights = args; | ||
case '[object Array]': | ||
servers = args.length ? args : [regular]; | ||
break; | ||
default: | ||
servers = args; | ||
servers.push(args || regular); | ||
break; | ||
@@ -160,3 +167,3 @@ } | ||
} | ||
, data: Utils.curry(memcached, privates.buffer, S) | ||
, data: curry(memcached, privates.buffer, S) | ||
, timeout: function streamTimeout () { | ||
@@ -474,3 +481,3 @@ Manager.remove(this); | ||
resultSet.forEach(function each (statSet) { | ||
response[statSet[0]] = statSet[1]; | ||
if (statSet) response[statSet[0]] = statSet[1]; | ||
}); | ||
@@ -495,6 +502,8 @@ | ||
resultSet.forEach(function each (statSet) { | ||
var identifier = statSet[0].split(':'); | ||
if (statSet) { | ||
var identifier = statSet[0].split(':'); | ||
if (!response[identifier[0]]) response[identifier[0]] = {}; | ||
response[identifier[0]][identifier[1]] = statSet[1]; | ||
if (!response[identifier[0]]) response[identifier[0]] = {}; | ||
response[identifier[0]][identifier[1]] = statSet[1]; | ||
} | ||
}); | ||
@@ -513,6 +522,8 @@ | ||
resultSet.forEach(function each (statSet) { | ||
var identifier = statSet[0].split(':'); | ||
if (statSet) { | ||
var identifier = statSet[0].split(':'); | ||
if (!response[identifier[1]]) response[identifier[1]] = {}; | ||
response[identifier[1]][identifier[2]] = statSet[1]; | ||
if (!response[identifier[1]]) response[identifier[1]] = {}; | ||
response[identifier[1]][identifier[2]] = statSet[1]; | ||
} | ||
}); | ||
@@ -694,3 +705,3 @@ | ||
, responses = {} | ||
, errors = null | ||
, errors = [] | ||
, calls; | ||
@@ -701,3 +712,2 @@ | ||
if (err) { | ||
errors = errors || []; | ||
errors.push(err); | ||
@@ -735,3 +745,2 @@ } | ||
var flag = 0 | ||
, memcached = this | ||
, valuetype = typeof value | ||
@@ -751,7 +760,7 @@ , length; | ||
length = Buffer.byteLength(value); | ||
if (length > memcached.maxValue) { | ||
return privates.errorResponse('The length of the value is greater than ' + memcached.maxValue, callback); | ||
if (length > this.maxValue) { | ||
return privates.errorResponse('The length of the value is greater than ' + this.maxValue, callback); | ||
} | ||
memcached.command(function settersCommand (noreply) { | ||
this.command(function settersCommand (noreply) { | ||
return { | ||
@@ -765,3 +774,3 @@ key: key | ||
, type: type | ||
, redundancyEnabled: true | ||
, redundancyEnabled: false | ||
, command: [type, key, flag, lifetime, length].join(' ') + | ||
@@ -776,4 +785,3 @@ (cas ? ' ' + cas : '') + | ||
// Curry the function and so we can tell the type our private set function | ||
memcached.set = Utils.curry(memcached | ||
, privates.setters | ||
memcached.set = curry(undefined, privates.setters | ||
, 'set' | ||
@@ -788,4 +796,3 @@ , [ | ||
memcached.replace = Utils.curry(memcached | ||
, privates.setters | ||
memcached.replace = curry(undefined, privates.setters | ||
, 'replace' | ||
@@ -800,4 +807,3 @@ , [ | ||
memcached.add = Utils.curry(memcached | ||
, privates.setters | ||
memcached.add = curry(undefined, privates.setters | ||
, 'add' | ||
@@ -882,4 +888,4 @@ , [ | ||
// Curry the function and so we can tell the type our private incrdecr | ||
memcached.increment = memcached.incr = Utils.curry(false, privates.incrdecr, 'incr'); | ||
memcached.decrement = memcached.decr = Utils.curry(false, privates.incrdecr, 'decr'); | ||
memcached.increment = memcached.incr = curry(undefined, privates.incrdecr, 'incr'); | ||
memcached.decrement = memcached.decr = curry(undefined, privates.incrdecr, 'decr'); | ||
@@ -938,8 +944,8 @@ // Deletes the keys from the servers | ||
// Curry the function and so we can tell the type our private singles | ||
memcached.version = Utils.curry(false, privates.singles, 'version'); | ||
memcached.flush = Utils.curry(false, privates.singles, 'flush_all'); | ||
memcached.stats = Utils.curry(false, privates.singles, 'stats'); | ||
memcached.settings = Utils.curry(false, privates.singles, 'stats settings'); | ||
memcached.slabs = Utils.curry(false, privates.singles, 'stats slabs'); | ||
memcached.items = Utils.curry(false, privates.singles, 'stats items'); | ||
memcached.version = curry(undefined, privates.singles, 'version'); | ||
memcached.flush = curry(undefined, privates.singles, 'flush_all'); | ||
memcached.stats = curry(undefined, privates.singles, 'stats'); | ||
memcached.settings = curry(undefined, privates.singles, 'stats settings'); | ||
memcached.slabs = curry(undefined, privates.singles, 'stats slabs'); | ||
memcached.items = curry(undefined, privates.singles, 'stats items'); | ||
@@ -946,0 +952,0 @@ // aliases |
@@ -64,12 +64,2 @@ "use strict"; | ||
// currys a function | ||
exports.curry = function curry (context, func) { | ||
var copy = Array.prototype.slice | ||
, args = copy.call(arguments, 2); | ||
return function () { | ||
return func.apply(context || this, args.concat(copy.call(arguments))); | ||
}; | ||
}; | ||
// a small util to use an object for eventEmitter | ||
@@ -92,2 +82,12 @@ exports.fuse = function fuse (target, handlers) { | ||
// curry/bind functions | ||
exports.curry = function curry (context, fn) { | ||
var copy = Array.prototype.slice | ||
, args = copy.call(arguments, 2); | ||
return function bowlofcurry () { | ||
return fn.apply(context || this, args.concat(copy.call(arguments))); | ||
}; | ||
}; | ||
// a small items iterator | ||
@@ -94,0 +94,0 @@ exports.Iterator = function iterator (collection, callback) { |
{ | ||
"name": "memcached", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"author": { | ||
@@ -43,2 +43,6 @@ "name": "Arnout Kazemier" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "*" | ||
}, | ||
"contributors": [ | ||
@@ -45,0 +49,0 @@ { |
Sorry, the diff of this file is not supported yet
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
259818
1562
2