node.js - 服务器上的事件连接数达到最大值

标签 node.js mongodb mongoose serverless-framework replicaset

我正在使用 mongodb 和 nodejs。我在 Atlas 上托管了 mongodb。

我的后端一直运行良好,但现在有时会卡住,当我在 mongodb atlas 上看到分析时,它显示最大事件连接数达到了 100

enter image description here

有人可以解释为什么会这样吗?我可以重新启动连接并使其成为 0 吗?

@Stennie 我用过 Mongoose 连接数据库

这是我的配置文件

const mongooseOptions = {
  useNewUrlParser: true,
  autoReconnect: true,
  poolSize: 25,
  connectTimeoutMS: 30000,
  socketTimeoutMS: 30000
} 

exports.register = (server, options, next) => {
  defaults = Hoek.applyToDefaults(defaults, options)

  if (Mongoose.connection.readyState) {
    return next()
  }

  if (!Mongoose.connection.readyState) {
    server.log(`${process.env.NOED_ENV} server connecting to ${defaults.url} ${defaults.url}`)

    return Mongoose.connect(defaults.url, mongooseOptions).then(() => {
      return next() // call the next item in hapi bootstrap
    })
  }
}

最佳答案

假设您的后端部署在 lambda 上,因为无服务器标签。

每次调用都会让容器闲置以防止冷启动,或者使用现有容器(如果可用)。您正在打开连接以在调用之间重用它,如 best practices 中所宣传的那样.

poolSize 为 25(?)和 100 个最大连接,您应该限制您的 function concurrency到 4。

Reserve concurrency to prevent your function from using all the available concurrency in the region, or from overloading downstream resources.

更多阅读:https://www.mongodb.com/blog/post/optimizing-aws-lambda-performance-with-mongodb-atlas-and-nodejs

关于node.js - 服务器上的事件连接数达到最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56373536/

相关文章:

Node.js 在生成后分离生成的子级

mongodb - 按索引从结果数组中获取一个元素

javascript - 处理可选参数的 Node 模块

arrays - 我可以检查是否仅在某个字段值尚未填充时才推送值吗?

javascript - 无法访问对象键值对

python - 如何从纬度转换为邮政编码或州以生成等值线 map

javascript - 获取另一个地理坐标和距离

javascript - 如何从javascript在mongodb中传递内部查询

javascript - require 路径中的变量不适用于 r.js

javascript - socket.io 错误 - 'Uncaught, unspecified "错误“事件。”