Created
May 20, 2012 17:14
-
-
Save tommedema/2758796 to your computer and use it in GitHub Desktop.
annotated mongodb node.js driver replicaset connection example, please read the comments
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
module.exports = function(mediator) { | |
var mongo = require('mongodb'), | |
Server = mongo.Server, | |
Db = mongo.Db, | |
ReplSet = mongo.ReplSet, | |
config = require('../../config'), | |
replSetName = config.mongo.rsname, | |
servers = config.mongo.servers, | |
dbName = config.mongo.dbname; | |
//create a connection on boot | |
mediator.once('boot.ready', function() { | |
//create replicaset servers array | |
var replSetServers = []; | |
servers.forEach(function(server) { | |
replSetServers.push(new Server(server.ip, server.port)); | |
}); | |
//create a new replicaset | |
var replSet = new ReplSet(replSetServers, { //http://mongodb.github.com/node-mongodb-native/markdown-docs/replicaset.html gives a different example where ReplSetServers is used instead of ReplSet, deprecated? | |
rs_name : replSetName, //the name of the replicaset to connect to. | |
ha : true, //turn on high availability --> I still have to test this, but so far its looking promising. | |
haInterval : 2000, //time between each replicaset status check | |
reconnectWait : 5000, //time to wait in miliseconds before attempting reconnect | |
retries : 1000, //number of times to attempt a replicaset reconnect. // --> how do I set this to unlimited? | |
readPreference : Server.READ_SECONDARY, //the prefered read preference (Server.READ_PRIMARY, Server.READ_SECONDARY, Server.READ_SECONDARY_ONLY) | |
poolSize : 4 //default poolSize for new server instances --> how do I determine the optimal pool size? | |
}); | |
//create the database | |
var db = new Db(dbName, replSet); | |
//open a connection to the database | |
db.open(function(err) { | |
if (err) return mediator.emit('db.error', err); | |
//event: database ready | |
mediator.emit('db.ready', db); | |
}); | |
}); | |
}; |
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
module.exports = function(mediator) { | |
var mongo = require('mongodb'), | |
Server = mongo.Server, | |
Db = mongo.Db, | |
ReplSet = mongo.ReplSet, | |
config = require('../../config'), | |
replSetName = config.mongo.rsname, | |
servers = config.mongo.servers, | |
dbName = config.mongo.dbname; | |
//create a connection on boot | |
mediator.once('boot.ready', function() { | |
//create replicaset servers array | |
var replSetServers = []; | |
servers.forEach(function(server) { | |
replSetServers.push(new Server(server.ip, server.port, { | |
auto_reconnect: true, //nullified by https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/connection/repl_set.js#L128 --> why is this forced off? does it still auto reconnect? | |
readPreference: Server.READ_SECONDARY, //probably redundant, sets the read preference (Server.READ_PRIMAR, Server.READ_SECONDARY_ONLY, Server.READ_SECONDARY) --> should this even be set here? | |
poolSize: 4 //probably redundant, number of connections in the connection pool --> should this even be set here, or in ReplSet creation? | |
})); | |
}); | |
//create a new replicaset | |
var replSet = new ReplSet(replSetServers, { //http://mongodb.github.com/node-mongodb-native/markdown-docs/replicaset.html gives a different example where ReplSetServers is used instead of ReplSet, deprecated? | |
rs_name : replSetName, //the name of the replicaset to connect to. | |
ha : true, //turn on high availability --> I still have to test this, but so far its looking promising. | |
haInterval : 2000, //time between each replicaset status check | |
reconnectWait : 5000, //time to wait in miliseconds before attempting reconnect | |
retries : 1000, //number of times to attempt a replicaset reconnect. // --> how do I set this to unlimited? | |
readPreference : Server.READ_SECONDARY, //the prefered read preference (Server.READ_PRIMARY, Server.READ_SECONDARY, Server.READ_SECONDARY_ONLY) | |
poolSize : 4 //default poolSize for new server instances --> how do I determine the optimal pool size? | |
}); | |
//create the database | |
var db = new Db(dbName, replSet, { | |
slaveOk: true, //is this still needed? same as readPreference? | |
reaper: true //why is this not enabled by default? seems like a must have for HA etc.? | |
}); | |
//open a connection to the database | |
db.open(function(err) { | |
if (err) return mediator.emit('db.error', err); | |
//event: database ready | |
mediator.emit('db.ready', db); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment