Skip to content

Instantly share code, notes, and snippets.

@johnnyhalife
Last active August 29, 2015 14:02
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 johnnyhalife/3c7c779b38d18c7bc8fd to your computer and use it in GitHub Desktop.
Save johnnyhalife/3c7c779b38d18c7bc8fd to your computer and use it in GitHub Desktop.
var MongoClient = require('mongodb').MongoClient;
var SERVER_OPTIONS = { auto_reconnect: true, poolSize: 4 };
var REPLSET_OPTIONS = { socketOptions: { keepAlive: 1, connectTimeoutMS: 0, socketTimeoutMS: 0 }, ha: true, haInterval: 1000 };
var OPTIONS = { db: { native_parser: false, numberOfRetries: 10 }, server: SERVER_OPTIONS, replSet: REPLSET_OPTIONS };
var current = null;
var retryCount = 0;
var await = [];
module.exports = function(callback) {
if(current) {
var poolConnected = (current.serverConfig.connectionPool && current.serverConfig.isConnected());
if(current.state == 'connected' || poolConnected) {
return callback(current);
}
current = null;
}
await.push(callback); // create a wait queue
if(await.length > 1) return;
connectToDB();
};
function connectToDB() {
MongoClient.connect(process.env.MONGOLAB_URI, OPTIONS, function(err, db) {
if(err) return onConnectionError(err);
// re-open on close
db.on('close', function() { db.open(function(err2){ throw err2; }); });
// return through the whole chain
await.forEach(function(cb) { cb(current = db); });
retryCount = 0;
await = [];
});
}
function onConnectionError(err) {
if(err && retryCount > 3) throw err;
retryCount += 1;
// wait a couple of seconds a stepDown
// may be happening right now. Although the switch
// is almost immediate there's some "Not master found" time.
setTimeout(connectToDB, 2 * 1000);
}
var MongoClient = require('mongodb').MongoClient;
var SERVER_OPTIONS = { auto_reconnect: true, poolSize: 4 };
var REPLSET_OPTIONS = { socketOptions: { keepAlive: 1, connectTimeoutMS: 0, socketTimeoutMS: 0 }, ha: true, haInterval: 1000 };
var OPTIONS = { db: { native_parser: false, numberOfRetries: 10 }, server: SERVER_OPTIONS, replSet: REPLSET_OPTIONS };
var current = null;
var retryCount = 0;
var await = [];
module.exports = function(callback) {
if(current) {
var poolConnected = (current.serverConfig.connectionPool && current.serverConfig.isConnected());
if(current.state == 'connected' || poolConnected) {
return callback(current);
}
current = null;
}
await.push(callback); // create a wait queue
if(await.length > 1) return;
connectToDB();
};
function connectToDB() {
MongoClient.connect(process.env.MONGOLAB_URI_SOURCE, OPTIONS, function(err, db) {
if(err) return onConnectionError(err);
// re-open on close
db.on('close', function() { db.open(function(err2){ throw err2; }); });
// return through the whole chain
await.forEach(function(cb) { cb(current = db); });
retryCount = 0;
await = [];
});
}
function onConnectionError(err) {
if(err && retryCount > 3) throw err;
retryCount += 1;
// wait a couple of seconds a stepDown
// may be happening right now. Although the switch
// is almost immediate there's some "Not master found" time.
setTimeout(connectToDB, 2 * 1000);
}
{
"name": "murally-webjobs",
"version": "0.0.1",
"private": true,
"dependencies": {
"request": "~2.34.0",
"azure": "~0.8.1",
"async": "~0.2.10",
"underscore": "~1.6.0",
"aws-sdk": "~2.0.0-rc10",
"tmp": "0.0.23",
"mime-of": "~0.1.0",
"mongodb": "~1.3.23",
"humanize": "0.0.9"
},
"engines": {
"node": "0.10.x",
"npm": ">=1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment