Created
July 6, 2012 14:54
-
-
Save izevaka/3060634 to your computer and use it in GitHub Desktop.
Connection pooling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
}); | |
}); |
Do you have an update on this? I suffer from same issue
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.
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
Hmm alright, might need to fork connect-redis and add ability to inject a pool. Seems like the connection eventually (after 30 minutes) enters a bad state that it doesn't recover from - [Error: Redis connection to failed - read ETIMEDOUT].