redis-connection-pool
Advanced tools
Comparing version 1.6.1 to 1.7.1
{ | ||
"name": "redis-connection-pool", | ||
"version": "1.6.1", | ||
"version": "1.7.1", | ||
"description": "a redis client connection pool", | ||
@@ -24,6 +24,5 @@ "license": "MIT", | ||
"dependencies": { | ||
"debug": "^2.1.0", | ||
"generic-pool": "^2.2.0", | ||
"q": "^1.4.1", | ||
"redis": "^2.6.2" | ||
"debug": "^4.0.1", | ||
"generic-pool": "^3.4.2", | ||
"redis": "^2.8.0" | ||
}, | ||
@@ -30,0 +29,0 @@ "devDependencies": { |
@@ -92,2 +92,7 @@ node-redis-connection-pool | ||
* **brpoplpush** | ||
```javascript | ||
brpoplpush(key1, key2, callback) | ||
``` | ||
* **blpop** | ||
@@ -94,0 +99,0 @@ ```javascript |
/** | ||
* redis-connection-pool.js | ||
* | ||
* copyright 2012 Nick Jennings (https://github.com/silverbucket) | ||
* copyright 2012 - 2018 Nick Jennings (https://github.com/silverbucket) | ||
* | ||
@@ -18,6 +18,5 @@ * licensed under the MIT license. | ||
var redis = require('redis'), | ||
Q = require('q'), | ||
Pool = require('generic-pool').Pool, | ||
debug = require('debug')('redis-connection-pool'); | ||
const redis = require('redis'), | ||
genericPool = require('generic-pool'), | ||
debug = require('debug')('redis-connection-pool'); | ||
@@ -65,3 +64,4 @@ /** | ||
function RedisConnectionPool(uid, cfg) { | ||
this.uid = (typeof uid ==='string') ? uid : 'redis-connection-pool-' + Math.floor((Math.random() * 99999) + 10000); | ||
this.uid = (typeof uid ==='string') ? uid : 'redis-connection-pool-' + | ||
Math.floor((Math.random() * 99999) + 10000); | ||
this.host = (typeof cfg.host === 'string') ? cfg.host : '127.0.0.1'; | ||
@@ -84,36 +84,41 @@ this.port = (typeof cfg.port === 'number') ? cfg.port : 6379; | ||
var self = this; | ||
let i = 0; | ||
const factory = { | ||
create: () => { | ||
return new Promise((resolve, reject) => { | ||
let client; | ||
if (this.url) { | ||
client = redis.createClient(this.url, this.options); | ||
} else { | ||
client = redis.createClient(this.port, this.host, this.options); | ||
} | ||
client.__name = `client${i}`; | ||
i = i + 1; | ||
var i = 0; | ||
this.pool = new Pool({ | ||
name: self.uid, | ||
create: function (callback) { | ||
var client; | ||
if (self.url) { | ||
client = redis.createClient(self.url, self.options); | ||
} else { | ||
client = redis.createClient(self.port, self.host, self.options); | ||
} | ||
client.__name = "client" + i; | ||
i = i + 1; | ||
this.database = this.database || 0; | ||
self.database = self.database || 0; | ||
debug('selecting database ' + this.database); | ||
client.on('error', function (err) { | ||
debug(err); | ||
}); | ||
debug('selecting database ' + self.database); | ||
client.on('error', function (err) { | ||
debug(err); | ||
}); | ||
client.on('ready', function () { | ||
client.select(self.database, function (err) { | ||
debug('2. selected database: ' + client.selected_db); | ||
callback(err, client); | ||
client.on('ready', () => { | ||
client.select(this.database, (err) => { | ||
debug('2. selected database: ' + client.selected_db); | ||
if (err) { return reject(err); } | ||
else { return resolve(client); } | ||
}); | ||
}); | ||
}); | ||
}, | ||
destroy: function (client) { | ||
return client.quit(); | ||
}, | ||
max: self.max_clients, | ||
log: false | ||
destroy: (client) => { | ||
return new Promise((resolve, reject) => { | ||
client.quit(); | ||
resolve(); | ||
}); | ||
} | ||
}; | ||
this.pool = genericPool.createPool(factory, { | ||
max: this.max_clients | ||
}); | ||
@@ -128,5 +133,5 @@ | ||
' waiting:' + pool.waitingClientsCount() + ']'); | ||
setTimeout(poolStats, 300000, pool); | ||
return setTimeout(poolStats, 300000, pool); | ||
}, 300000, this.pool); | ||
return this; | ||
@@ -148,4 +153,4 @@ } | ||
*/ | ||
RedisConnectionPool.prototype.on = function(type, cb) { | ||
var client = redis.createClient(); | ||
RedisConnectionPool.prototype.on = function (type, cb) { | ||
const client = redis.createClient(); | ||
client.on(type, cb); | ||
@@ -163,9 +168,8 @@ }; | ||
RedisConnectionPool.prototype.serverInfo = function (cb) { | ||
var pool = this.pool; | ||
pool.acquire(function (err, client) { | ||
var serverInfo = client.server_info; | ||
this.pool.acquire().then((client) => { | ||
let serverInfo = client.server_info; | ||
serverInfo.database = client.selected_db; | ||
pool.release(client); | ||
this.pool.release(client); | ||
cb(null, serverInfo); | ||
}); | ||
}).catch(cb); | ||
}; | ||
@@ -378,2 +382,18 @@ | ||
/** | ||
* Function: brpoplpush | ||
* | ||
* Execute a redis BRPOPLPUSH command | ||
* | ||
* Parameters: | ||
* | ||
* key1 - (string) - The pop list key | ||
* key2 - (string) - The push list key | ||
* cb - (function) - Callback to be executed on completion | ||
* | ||
*/ | ||
RedisConnectionPool.prototype.brpoplpush = function (key1, key2, cb) { | ||
_getFuncs.apply(this, ['brpoplpush', [key1, key2], cb]); | ||
}; | ||
/** | ||
* Function: clean | ||
@@ -391,14 +411,13 @@ * | ||
debug('clearing redis key ' + key); | ||
var client = redis.createClient(); | ||
var self = this; | ||
const client = redis.createClient(); | ||
client.keys(key, function (err, keys) { | ||
client.keys(key, (err, keys) => { | ||
client.quit(); | ||
if ((keys) && (keys.forEach)) { | ||
keys.forEach(function (name) { | ||
keys.forEach((name) => { | ||
debug('deleting name ' + name); | ||
self.del(name); | ||
this.del(name); | ||
}); | ||
} else { | ||
debug('ERROR couldnt get keys list on key \'' + key + '\': ', keys); | ||
debug(`ERROR couldnt get keys list on key '${key}': `, keys); | ||
} | ||
@@ -461,4 +480,3 @@ if (err) { | ||
function redisSingle (funcName, key, val, cb) { | ||
var pool = this.pool; | ||
function redisSingle(funcName, key, val, cb) { | ||
if (typeof val === 'function') { | ||
@@ -468,7 +486,7 @@ cb = val; | ||
} | ||
pool.acquire(function (err, client) { | ||
this.pool.acquire().then((client) => { | ||
if (funcName === 'hdel') { | ||
var args = [key].concat(val); | ||
client[funcName](args, function (err, reply) { | ||
pool.release(client); | ||
const args = [key].concat(val); | ||
client[funcName](args, (err, reply) => { | ||
this.pool.release(client); | ||
if (typeof cb === 'function') { | ||
@@ -479,4 +497,4 @@ cb(err, reply); | ||
} else if (val) { | ||
client[funcName](key, val, function (err, reply) { | ||
pool.release(client); | ||
client[funcName](key, val, (err, reply) => { | ||
this.pool.release(client); | ||
if (typeof cb === 'function') { | ||
@@ -487,4 +505,4 @@ cb(err, reply); | ||
} else { | ||
client[funcName](key, function (err, reply) { | ||
pool.release(client); | ||
client[funcName](key, (err, reply) => { | ||
this.pool.release(client); | ||
if (typeof cb === 'function') { | ||
@@ -495,3 +513,3 @@ cb(err, reply); | ||
} | ||
}); | ||
}).catch(cb); | ||
} | ||
@@ -501,4 +519,2 @@ | ||
function _setFuncs(funcName, key, field, data, cb) { | ||
var pool = this.pool; | ||
if (typeof cb === 'undefined') { | ||
@@ -510,8 +526,8 @@ cb = data; | ||
pool.acquire(function (err, client) { | ||
this.pool.acquire().then((client) => { | ||
if (funcName === 'hset') { | ||
client[funcName](key, field, data, function (err, reply) { | ||
pool.release(client); | ||
client[funcName](key, field, data, (err, reply) => { | ||
this.pool.release(client); | ||
if (err) { | ||
debug("ERROR " + funcName + ": " + err); | ||
debug(`ERROR ${funcName}: ` + err); | ||
} | ||
@@ -523,6 +539,6 @@ if (typeof cb === 'function') { | ||
} else if (funcName === 'set') { | ||
client[funcName](key, data, function (err, reply) { | ||
pool.release(client); | ||
client[funcName](key, data, (err, reply) => { | ||
this.pool.release(client); | ||
if (err) { | ||
debug("ERROR " + funcName + ": " + err); | ||
debug(`ERROR ${funcName}: ` + err); | ||
} | ||
@@ -534,6 +550,6 @@ if (typeof cb === 'function') { | ||
} else { | ||
client[funcName](key, data, function (err, reply) { | ||
pool.release(client); | ||
client[funcName](key, data, (err, reply) => { | ||
this.pool.release(client); | ||
if (err) { | ||
debug("ERROR " + err); | ||
debug('ERROR ' + err); | ||
} | ||
@@ -545,3 +561,3 @@ if (typeof cb === 'function') { | ||
} | ||
}); | ||
}).catch(cb); | ||
} | ||
@@ -551,4 +567,2 @@ | ||
function _getFuncs(funcName, key, field, cb) { | ||
var pool = this.pool; | ||
var self = this; | ||
if ((typeof field === 'function') && (typeof cb === 'undefined')) { | ||
@@ -559,15 +573,18 @@ cb = field; | ||
pool.acquire(function (err, client) { | ||
if ((funcName === 'get') || (funcName === 'hgetall') || (funcName === 'ttl') || (funcName === 'incr')) { | ||
redisGet.apply(self, [funcName, client, key, cb]); | ||
this.pool.acquire().then((client) => { | ||
if ((funcName === 'get') || (funcName === 'hgetall') || | ||
(funcName === 'ttl') || (funcName === 'incr')) { | ||
redisGet.apply(this, [funcName, client, key, cb]); | ||
} else if (funcName === 'blpop') { | ||
redisBlockingGet.apply(self, ['blpop', client, key, cb]); | ||
redisBlockingGet.apply(this, ['blpop', client, key, cb]); | ||
} else if (funcName === 'brpop') { | ||
redisBlockingGet.apply(self, ['brpop', client, key, cb]); | ||
redisBlockingGet.apply(this, ['brpop', client, key, cb]); | ||
} else if (funcName === 'brpoplpush') { | ||
redisBlockingGetBRPOPLPUSH.apply(this, ['brpoplpush', client, key[0], key[1], cb]); | ||
} else if (funcName === 'hget') { | ||
redisHashGet.apply(self, [client, key, field, cb]); | ||
redisHashGet.apply(this, [client, key, field, cb]); | ||
} else if (funcName === 'hgetall') { | ||
redisHashGet.apply(self, [client, key, null, cb]); | ||
redisHashGet.apply(this, [client, key, null, cb]); | ||
} | ||
}); | ||
}).catch(cb); | ||
} | ||
@@ -578,7 +595,6 @@ | ||
function redisGet(funcName, client, key, cb) { | ||
var responded = false; | ||
var pool = this.pool; | ||
client[funcName](key, function (err, replies) { | ||
let responded = false; | ||
client[funcName](key, (err, replies) => { | ||
responded = true; | ||
pool.release(client); | ||
this.pool.release(client); | ||
if (err) { | ||
@@ -592,6 +608,6 @@ debug('ERROR: redis error (' + funcName + ' ' + key + ')', err); | ||
setTimeout(function() { | ||
setTimeout(() => { | ||
if (!responded) { | ||
debug('ERROR: redis.' + funcName+' never returned (5s), destroying connection. ' + key); | ||
pool.destroy(client); | ||
this.pool.destroy(client); | ||
} | ||
@@ -603,6 +619,5 @@ }, 5000); | ||
function redisHashGet(client, key, field, cb) { | ||
var pool = this.pool; | ||
if (field) { | ||
client.hget(key, field, function (err, replies) { | ||
pool.release(client); | ||
client.hget(key, field, (err, replies) => { | ||
this.pool.release(client); | ||
if (err) { | ||
@@ -616,4 +631,4 @@ debug('ERROR: redis error (hget ' + key + ')', err); | ||
} else { | ||
client.hgetall(key, function (err, replies) { | ||
pool.release(client); | ||
client.hgetall(key, (err, replies) => { | ||
this.pool.release(client); | ||
if (err) { | ||
@@ -631,7 +646,6 @@ debug('ERROR: redis error (hget ' + key + ')', err); | ||
function redisBlockingGet(funcName, client, key, cb) { | ||
var pool = this.pool; | ||
var responded = false; | ||
client[funcName](key, 0, function (err, replies) { | ||
let responded = false; | ||
client[funcName](key, 0, (err, replies) => { | ||
responded = true; | ||
pool.release(client); | ||
this.pool.release(client); | ||
if (err) { | ||
@@ -649,39 +663,55 @@ debug('ERROR (' + funcName + ')', err); | ||
function redisBlockingGetBRPOPLPUSH(funcName, client, key1, key2, cb) { | ||
let responded = false; | ||
client[funcName](key1, key2, 0, (err, replies) => { | ||
responded = true; | ||
this.pool.release(client); | ||
if (err) { | ||
debug('ERROR (' + funcName + ')', err); | ||
cb(err, null); | ||
} else if ((!replies) || (typeof replies[1] === 'undefined')) { | ||
debug('ERROR got a bad reply: ', replies); | ||
cb('got bad reply from redis', []); | ||
} else { | ||
cb(err, replies); | ||
} | ||
}); | ||
} | ||
function redisCheck() { | ||
var q = Q.defer(); | ||
var self = this; | ||
var client; | ||
if (self.url) { | ||
client = redis.createClient(self.url); | ||
} else { | ||
client = redis.createClient(self.port, self.host); | ||
} | ||
try { | ||
client.on('error', function (err) { | ||
return new Promise((resolve, reject) => { | ||
let client; | ||
if (this.url) { | ||
client = redis.createClient(this.url, this.options); | ||
} else { | ||
client = redis.createClient(this.port, this.host, this.options); | ||
} | ||
try { | ||
client.on('error', (err) => { | ||
client.quit(); | ||
reject(err); | ||
}); | ||
client.on('ready', () => { | ||
client.server_info = client.server_info || {}; | ||
this.version_string = client.server_info.redis_version; | ||
this.version_array = client.server_info.versions; | ||
if (!this.version_array || this.version_array[0] < 2) { | ||
this.blocking_support = false; | ||
} | ||
client.quit(); | ||
resolve(this.version_string); | ||
}); | ||
} catch (e) { | ||
debug('ERROR cannot connect to redis, ' + e); | ||
client.quit(); | ||
q.reject(err); | ||
}); | ||
client.on('ready', function () { | ||
client.server_info = client.server_info || {}; | ||
self.version_string = client.server_info.redis_version; | ||
self.version_array = client.server_info.versions; | ||
if (!self.version_array || self.version_array[0] < 2) { | ||
self.blocking_support = false; | ||
} | ||
client.quit(); | ||
q.resolve(self.version_string); | ||
}); | ||
} catch (e) { | ||
debug('ERROR cannot connect to redis, ' + e); | ||
q.reject('cannot connect to redis: ' + e); | ||
client.quit(); | ||
} | ||
return q.promise; | ||
reject('cannot connect to redis: ' + e); | ||
} | ||
}); | ||
} | ||
var redisConnectionPoolWrapper; | ||
let redisConnectionPoolWrapper; | ||
(function () { | ||
var redisConnectionPools = {}; | ||
let redisConnectionPools = {}; | ||
redisConnectionPoolWrapper = function (uid, cfg) { | ||
@@ -691,3 +721,3 @@ if (typeof redisConnectionPools[uid] === 'object') { | ||
} else { | ||
var redisConnectionPool = new RedisConnectionPool(uid, cfg); | ||
const redisConnectionPool = new RedisConnectionPool(uid, cfg); | ||
redisConnectionPools[redisConnectionPool.uid] = redisConnectionPool; | ||
@@ -709,2 +739,1 @@ return redisConnectionPool; | ||
}; | ||
@@ -6,13 +6,11 @@ if (typeof define !== 'function') { | ||
define(['require'], function (require) { | ||
var suites = []; | ||
let suites = []; | ||
suites.push( | ||
{ | ||
name: "database connection tests", | ||
desc: "testing states of database connectivity", | ||
suites.push({ | ||
name: 'database connection tests', | ||
desc: 'testing states of database connectivity', | ||
abortOnFail: true, // don't continue with further test suites if any tests in this suite fail | ||
setup: function (env, test) { | ||
env.RedisPool = require('./../src/redis-connection-pool'); | ||
env.channel = "redis-connection-pool-tests:"; | ||
env.channel = 'redis-connection-pool-tests:'; | ||
test.done(); | ||
@@ -97,3 +95,3 @@ }, | ||
env.redisPool = env.RedisPool('redisPoolTestsURL', { | ||
url: "redis://localhost:6379", | ||
url: 'redis://localhost:6379', | ||
max_clients: 12, | ||
@@ -103,3 +101,3 @@ perform_checks: true | ||
test.assertAnd(env.redisPool.url, 'redis://localhost:6379'); | ||
test.assertTypeAnd(env.redisPool.host, "undefined"); | ||
test.assertTypeAnd(env.redisPool.host, 'undefined'); | ||
test.assert(env.redisPool.max_clients, 12); | ||
@@ -122,7 +120,7 @@ | ||
{ | ||
name: "redis tests", | ||
desc: "collection of basic redis-connection-pool tests", | ||
name: 'redis tests', | ||
desc: 'collection of basic redis-connection-pool tests', | ||
abortOnFail: true, // don't continue with further test suites if any tests in this suite fail | ||
setup: function (env, test) { | ||
env.channel = "redis-connection-pool-tests:"; | ||
env.channel = 'redis-connection-pool-tests:'; | ||
env.redisPool = require('./../src/redis-connection-pool')('redisPoolTests', { | ||
@@ -149,3 +147,3 @@ host: '127.0.0.1', | ||
{ | ||
desc: "#check()", | ||
desc: '#check()', | ||
timeout: 2000, | ||
@@ -160,3 +158,3 @@ run: function (env, test) { | ||
{ | ||
desc: "verify version properties are set", | ||
desc: 'verify version properties are set', | ||
timeout: 2000, | ||
@@ -170,3 +168,3 @@ run: function (env, test) { | ||
{ | ||
desc: "#set", | ||
desc: '#set', | ||
timeout: 2000, | ||
@@ -181,3 +179,3 @@ run: function (env, test) { | ||
{ | ||
desc: "#get", | ||
desc: '#get', | ||
run: function (env, test) { | ||
@@ -192,3 +190,3 @@ env.redisPool.get(env.channel + 'test', function (err, reply) { | ||
{ | ||
desc: "#hset", | ||
desc: '#hset', | ||
run: function (env, test) { | ||
@@ -202,3 +200,3 @@ env.redisPool.hset(env.channel + 'testhash', 'foo', 'bar', function (err, reply) { | ||
{ | ||
desc: "#hget", | ||
desc: '#hget', | ||
run: function (env, test) { | ||
@@ -213,3 +211,3 @@ env.redisPool.hget(env.channel + 'testhash', 'foo', function (err, reply) { | ||
{ | ||
desc: "#hset", | ||
desc: '#hset', | ||
run: function (env, test) { | ||
@@ -232,3 +230,3 @@ env.redisPool.hset(env.channel + 'testhash', 'foo', 'bar', function (err, reply) { | ||
{ | ||
desc: "#hgetall", | ||
desc: '#hgetall', | ||
run: function (env, test) { | ||
@@ -244,3 +242,3 @@ env.redisPool.hget(env.channel + 'testhash', function (err, reply) { | ||
{ | ||
desc: "#rpush", | ||
desc: '#rpush', | ||
run: function (env, test) { | ||
@@ -260,3 +258,3 @@ env.redisPool.rpush(env.channel + 'testlist', 'foo', function (err, reply) { | ||
{ | ||
desc: "#blpop", | ||
desc: '#blpop', | ||
run: function (env, test) { | ||
@@ -271,3 +269,3 @@ env.redisPool.blpop(env.channel + 'testlist', function (err, reply) { | ||
{ | ||
desc: "#brpop", | ||
desc: '#brpop', | ||
run: function (env, test) { | ||
@@ -282,3 +280,3 @@ env.redisPool.brpop(env.channel + 'testlist', function (err, reply) { | ||
{ | ||
desc: "#lpush", | ||
desc: '#lpush', | ||
run: function (env, test) { | ||
@@ -298,3 +296,3 @@ env.redisPool.lpush(env.channel + 'testlist', 'foo', function (err, reply) { | ||
{ | ||
desc: "#blpop", | ||
desc: '#blpop', | ||
run: function (env, test) { | ||
@@ -309,3 +307,3 @@ env.redisPool.blpop(env.channel + 'testlist', function (err, reply) { | ||
{ | ||
desc: "#brpop", | ||
desc: '#brpop', | ||
run: function (env, test) { | ||
@@ -320,3 +318,3 @@ env.redisPool.brpop(env.channel + 'testlist', function (err, reply) { | ||
{ | ||
desc: "#expire", | ||
desc: '#expire', | ||
timeout: 2000, | ||
@@ -331,3 +329,3 @@ run: function (env, test) { | ||
{ | ||
desc: "#ttl", | ||
desc: '#ttl', | ||
timeout: 2000, | ||
@@ -343,3 +341,3 @@ run: function (env, test) { | ||
{ | ||
desc: "#set", | ||
desc: '#set', | ||
timeout: 2000, | ||
@@ -354,3 +352,3 @@ run: function (env, test) { | ||
{ | ||
desc: "#incr", | ||
desc: '#incr', | ||
timeout: 2000, | ||
@@ -357,0 +355,0 @@ run: function (env, test) { |
Sorry, the diff of this file is not supported yet
300952
3
3795
123
+ Addeddebug@4.3.5(transitive)
+ Addedgeneric-pool@3.9.0(transitive)
+ Addedms@2.1.2(transitive)
- Removedq@^1.4.1
- Removeddebug@2.6.9(transitive)
- Removedgeneric-pool@2.5.4(transitive)
- Removedms@2.0.0(transitive)
- Removedq@1.5.1(transitive)
Updateddebug@^4.0.1
Updatedgeneric-pool@^3.4.2
Updatedredis@^2.8.0