Skip to content

Instantly share code, notes, and snippets.

@guotingchao
Last active December 15, 2015 06:49
Show Gist options
  • Save guotingchao/5219372 to your computer and use it in GitHub Desktop.
Save guotingchao/5219372 to your computer and use it in GitHub Desktop.
mongodb的坑
//Query, Insert, Update are worked, but CPU load very high in ReplSet mode!
//Explore the cause of the problem
//ACL would blocked all unauthorized network access.
//I found out the problem, it eat the CPU by /lib/mongodb/connection/connection_pool.js.
connection.on("connect", function(err, connection) {
// Add connection to list of open connections
_self.openConnections.push(connection);
// If the number of open connections is equal to the poolSize
if(_self.openConnections.length === _self.poolSize && _self._poolState !== 'disconnected') {
// Set connected
_self._poolState = 'connected';
// Emit pool ready
_self.emit("poolReady");
} else if(_self.openConnections.length < _self.poolSize) {
// need to open another connection, make sure it's in the next
// tick so we don't get a cascade of errors
process.nextTick(function() {
_connect(_self);
});
}
});
//when openConnections.length < poolSize, pool will create a new connection in nextTick.
//But in meantime, if the openning connection emit('close'), this openConnections.length will be cleanup to 0.
//connection.on("close", function() {
// If we are already disconnected ignore the event
if(_self._poolState !== 'disconnected' && _self.listeners("close").length > 0) {
_self.emit("close");
}
// Set disconnected
_self._poolState = 'disconnected';
// Stop
_self.stop();
});
//This case will be Infinite loop, open, close, open, close...
//How to fixed this?
//Waiting for your pull request on node-mongodb-native
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment