【发布时间】:2016-03-29 16:14:42
【问题描述】:
我有一个四核系统,我试图在 MEAN 堆栈上运行应用程序。当我使用猫鼬驱动程序时,我已将 poolSize 设置为 5。但是我看到在 monogod 日志中打开了 40 多个连接。如果没有集群,我注意到 poolSize 工作正常,我只看到 5 个连接打开。
集群的连接问题
2016-03-28T16:32:00.404+0530 I NETWORK [conn14] end connection 127.0.0.1:33925 (31 connections now open)
2016-03-28T16:32:00.404+0530 I NETWORK [conn13] end connection 127.0.0.1:33924 (31 connections now open)
2016-03-28T16:32:00.404+0530 I NETWORK [conn4] end connection
我的server.js代码如下
var app = express();
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i += 1) {
cluster.fork();
}
cluster.on('exit', function (worker) {
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
}
else {
/**
* Connect to MongoDB.
*/
dalUtils.connectToDatabase(secrets.env);
/**
* Express configuration.
*/
}
我的dalutils代码如下
var mongoose = require('mongoose');
var secrets = require('../../config/secrets');
exports.connectToDatabase = function(environment){
mongoose.connection.on('error', function() {
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
});
mongoose.connection.on('connected', function() {
console.log('Connected!!!');
});
switch (environment) {
case 'development':
mongoose.connect(secrets.dev_db,{ server: { poolSize: 5 }});
break;
case 'production' :
mongoose.connect(secrets.prod_db);
break;
default :
mongoose.connect(secrets.dev_db);
break;
}
};
【问题讨论】:
标签: node.js mongodb mongoose mean-stack