Skip to content

Instantly share code, notes, and snippets.

@cggaurav
Created December 16, 2013 16:30
Show Gist options
  • Save cggaurav/7989909 to your computer and use it in GitHub Desktop.
Save cggaurav/7989909 to your computer and use it in GitHub Desktop.
Redis Connection Pool
'use strict';
var uri = require('../util/url');
var pool = require('generic-pool');
var driver = require('redis');
var flag = require('node-env-flag');
module.exports = function(url) {
var connection_settings = uri(url || process.env.REDIS_URL);
return pool.Pool({
name: 'redis',
create: function(callback) {
var redis = require('redis');
var client = redis.createClient(connection_settings.port, connection_settings.hostname, connection_settings.server);
if (connection_settings.password) {
client.auth(connection_settings.password, function(err, reply) {
if (err) {
callback(err, null);
return;
}
callback(null, client);
});
} else {
callback(null, client);
}
},
destroy: function(client) {
client.end();
},
max: process.env.POOL_MAX_REDIS || 10,
min: process.env.POOL_MIN_REDIS || 1,
idleTimeoutMillis: process.env.POOL_TIMEOUT_REDIS || 10000,
log: flag(process.env.POOL_LOG_REDIS, false)
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment