我正在使用 mongodb 和 nodejs。我在 Atlas 上托管了 mongodb。
我的后端一直运行良好,但现在有时会卡住,当我在 mongodb atlas 上看到分析时,它显示最大事件连接数达到了 100。
有人可以解释为什么会这样吗?我可以重新启动连接并使其成为 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/