Created
December 16, 2013 16:31
-
-
Save cggaurav/7989927 to your computer and use it in GitHub Desktop.
MongoDB Connection Pool
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
'use strict'; | |
var uri = require('../util/url'); | |
var pool = require('generic-pool'); | |
var driver = require('mongodb'); | |
var flag = require('node-env-flag'); | |
module.exports = function(url) { | |
var connection_settings = uri(url || process.env.MONGODB_URL); | |
return pool.Pool({ | |
name: 'mongodb', | |
create: function(callback) { | |
var server = new driver.Server(connection_settings.hostname, connection_settings.port, {}); | |
var client = new driver.Db(connection_settings.db, server, {safe: false}); | |
client.open(function(err, p_client) { | |
if (err) { | |
callback(err, null); | |
return; | |
} | |
if (connection_settings.username && connection_settings.password) { | |
client.authenticate(connection_settings.username, connection_settings.password, function(err, replies) { | |
if (err) { | |
callback(err, null); | |
return; | |
} | |
callback(null, client); | |
}); | |
} else { | |
callback(null, client); | |
} | |
}); | |
}, | |
destroy: function(client) { | |
client.close(); | |
}, | |
max: process.env.POOL_MAX_MONGODB || 10, | |
min: process.env.POOL_MIN_MONGODB || 1, | |
idleTimeoutMillis: process.env.POOL_TIMEOUT_MONGODB || 10000, | |
log: flag(process.env.POOL_LOG_MONGODB, false) | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment