Skip to content

Instantly share code, notes, and snippets.

@izevaka
Created July 6, 2012 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izevaka/3060634 to your computer and use it in GitHub Desktop.
Save izevaka/3060634 to your computer and use it in GitHub Desktop.
Connection pooling
//Problem: In node.js, one must pool redis db connections, otherwise they die
//Solution: Use generic-pool
pool = poolModule.Pool({
name: 'redis',
create: function (callback) {
var redisClient = redis.createClient();
callback(null, redisClient);
},
destroy : function (client) {
client.quit();
},
max: 10,
min: 0,
idleTimeoutMillis : 4000,
log : false
});
//Using the pool
pool.acquire(function (err, client){
client.hgetall("hash", function (err, data) {
pool.release(client);
//do something with data
});
});
//Problem 2: Hide connection pooling from app code
//Solution - use pool.pooled
var pooledRedis = {
hgetall: pool.pooled(function (client, hashName, cb) {
client.hgetall(hashName, cb);
}),
lrange: pool.pooled(function (client, key, start, length, cb) {
client.lrange(key, start, length, cb);
})
};
//Problem 3: Duplication, ugly
//Solution - some argument slicin'
var methods = ["hgetall", "lrange"];
methods.forEach(function (funcName) {
pooledRedis[funcName] = pool.pooled(function (client) {
var args = Array.prototype.slice.call(arguments, 1, arguments.length);
client[funcName].apply(client, args);
});
});
@tacomilkshake
Copy link

I too am having this issue; it's odd I seemed to have the issue using my own redis server on Joyent, but not using RedisToGo, which makes me wonder if there's something in the Redis configuration that can resolve it.

@waylonflinn
Copy link

I seem to be having this issue too. After about 20 minutes my entire app/server becomes unresponsive. After a slightly variable but usually large amount of time I get [Redis connection to failed - read ETIMEDOUT] and it starts working again.

Anyone have any more information about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment