MongoDB Node.js Lambda连接已最大化

时间:2019-02-14 15:08:00

标签: node.js mongodb amazon-web-services aws-lambda database-connection

我的MongoDB地图集有问题。我正在运行Atlas 3.4版实例M20版。我正在使用的环境是AWS Lambda架构,其中有几个查询数据库的服务。由于某种原因,数据库在几分钟内达到其最大连接数(700个连接),然后崩溃。

当前,我正在使用这组选项来运行数据库:

我尝试了一些在网上找到的解决方案-使用

context.callbackWaitsForEmptyEventLoop = false;

并使用dbpools将数据库设置为单例,但仍然无法保持低连接。

const mongo = require('mongodb').MongoClient;
const log = require('./logger');

let cachedDbs = {};

module.exports.get = async uri => {
  let cachedDb = cachedDbs[uri];

  if (cachedDb && cachedDb.serverConfig.isConnected()) {
    return cachedDb;
  }

  log.info('Getting db connection to', uri);

  const client = await mongo.connect(uri, {
    useNewUrlParser: true,
    appname: process.env.SERVICE_NAME,
    reconnectTries: 5,
    poolSize: 5,
    maxIdleTimeMS: 1000,
    socketTimeoutMS: 1000,
    waitQueueTimeoutMS: 1000
  });
  cachedDb = await client.db();
  cachedDbs[uri] = cachedDb;

  return cachedDb;
};

以前有人遇到过这个问题吗?

预先感谢

0 个答案:

没有答案