var / log / mongodb / mongod.log来自node.js的无限循环的打开和关闭连接

时间:2017-07-14 17:58:28

标签: javascript node.js mongodb logging

/var/log/mongodb/mongod.log内部我有一个无限循环的开放和关闭连接。它使mongod.log变得庞大,占用了磁盘空间。我还担心我的node.js应用程序中存在错误的代码。我在我的节点js应用程序中使用mongoose和本机mongo驱动程序。我没有看到任何错误或例外。

有没有人见过这个,可能是根本原因?日志现在是11GB并且不断增长。

2017-07-14T17:51:59.562+0000 I -        [conn21293746] end connection 127.0.0.1:49373 (19 connections now open)
2017-07-14T17:51:59.638+0000 I NETWORK  [thread1] connection accepted from 127.0.0.1:49374 #21293747 (19 connections now open)
2017-07-14T17:51:59.639+0000 I NETWORK  [conn21293747] received client metadata from 127.0.0.1:49374 conn21293747: { driver: { name: "nodejs", version: "2.2.28" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "3.13.0-117-generic" }, platform: "Node.js v6.10.2, LE, mongodb-core: 2.1.12" }

2017-07-14T17:51:59.641+0000 I -        [conn21293747] end connection 127.0.0.1:49374 (19 connections now open)
2017-07-14T17:52:00.008+0000 I NETWORK  [thread1] connection accepted from 127.0.0.1:49375 #21293748 (19 connections now open)
2017-07-14T17:52:00.008+0000 I NETWORK  [conn21293748] received client metadata from 127.0.0.1:49375 conn21293748: { driver: { name: "nodejs", version: "2.2.28" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "3.13.0-117-generic" }, platform: "Node.js v6.10.2, LE, mongodb-core: 2.1.12" }
2017-07-14T17:52:00.013+0000 I -        [conn21293748] end connection 127.0.0.1:49375 (19 connections now open)
2017-07-14T17:52:00.547+0000 I NETWORK  [thread1] connection accepted from 127.0.0.1:49376 #21293749 (19 connections now open)
2017-07-14T17:52:00.548+0000 I NETWORK  [conn21293749] received client metadata from 127.0.0.1:49376 conn21293749: { driver: { name: "nodejs", version: "2.2.28" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "3.13.0-117-generic" }, platform: "Node.js v6.10.2, LE, mongodb-core: 2.1.12" }
2017-07-14T17:52:00.550+0000 I -        [conn21293749] end connection 127.0.0.1:49376 (19 connections now open)

经常调用的一些特定node.js代码

var MongoClient = require('mongodb').MongoClient;
exports.logSession  = function(payload)
{
    MongoClient.connect(url, function (err, db) {
        assert.equal(null, err);
        insertSessionIntoTable(payload, db, function () {
            db.close();
        })

    })

};

function insertSessionIntoTable(payload, db, callback) {
db.collection(collectionName).insertOne(payload, function (err, result) {
    assert.equal(err, null);
    logger.verbose("Inserted a document into the collection.");
    callback();
});

1 个答案:

答案 0 :(得分:0)

您的代码将为每个会话打开连接

exports.logSession  = function(payload)
{
    MongoClient.connect(url, function (err, db) { // for every session your code will open new connection 
        assert.equal(null, err);
        insertSessionIntoTable(payload, db, function () {
            db.close(); // here it will close it
        })

    })
};

打开连接一次并使用相同的连接执行所有操作。

var db;
MongoClient.connect(url)
  .then(function (dbInstance) { // <- db as first argument
    db = dbInstance;
  })
  .catch(function (err) {})

exports.logSession  = function(payload)
{
        insertSessionIntoTable(payload, db, function () {
            // work done
        })

    })
};