Pho*_*x20 5 lambda mongoose mongodb node.js
我有一个使用 mongoose 查询 mongoDb 的节点 lambda 函数。
大约 20% 的时间,看似随机,我在尝试连接时会收到以下错误: MongoNetworkTimeoutError: 连接超时
虽然 MongoDb 似乎建议使用 context.callbackWaitsForEmptyEventLoop = false 并尝试在调用之间重用相同的连接,但我读到其他帖子说解决此问题的方法是每次都主动重新打开连接。我尝试过,但它仍然发生。有人有什么想法吗?
这是我的代码:
let conn = mongoose.createConnection(process.env.MONGO_URI, {
bufferCommands: false, // Disable mongoose buffering
bufferMaxEntries: 0, // and MongoDB driver buffering
useNewUrlParser: true,
useUnifiedTopology: true,
socketTimeoutMS: 45000,
keepAlive: true,
reconnectTries: 10
})
try {
await conn
console.log('Connected correctly to server')
} catch (err) {
console.log('Error connecting to DB')
console.log(err)
console.log(err.stack)
}
await conn
Run Code Online (Sandbox Code Playgroud)
以下是 Cloudwatch 的完整错误输出:
{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "MongoNetworkTimeoutError: connection timed out",
"reason": {
"errorType": "MongoNetworkTimeoutError",
"errorMessage": "connection timed out",
"name": "MongoNetworkTimeoutError",
"stack": [
"MongoNetworkTimeoutError: connection timed out",
" at connectionFailureError (/var/task/node_modules/mongodb/lib/core/connection/connect.js:342:14)",
" at TLSSocket.<anonymous> (/var/task/node_modules/mongodb/lib/core/connection/connect.js:310:16)",
" at Object.onceWrapper (events.js:420:28)",
" at TLSSocket.emit (events.js:314:20)",
" at TLSSocket.EventEmitter.emit (domain.js:483:12)",
" at TLSSocket.Socket._onTimeout (net.js:484:8)",
" at listOnTimeout (internal/timers.js:554:17)",
" at processTimers (internal/timers.js:497:7)"
]
},
"promise": {},
"stack": [
"Runtime.UnhandledPromiseRejection: MongoNetworkTimeoutError: connection timed out",
" at process.<anonymous> (/var/runtime/index.js:35:15)",
" at process.emit (events.js:326:22)",
" at process.EventEmitter.emit (domain.js:483:12)",
" at processPromiseRejections (internal/process/promises.js:209:33)",
" at processTicksAndRejections (internal/process/task_queues.js:98:32)",
" at runNextTicks (internal/process/task_queues.js:66:3)",
" at listOnTimeout (internal/timers.js:523:9)",
" at processTimers (internal/timers.js:497:7)"
]
}
Run Code Online (Sandbox Code Playgroud)
小智 2
我遇到了同样的问题(我有一个快速应用程序,但这并不重要)。解决方案是将数据库连接对象移到处理程序方法之外并缓存/重用它。
\n\'use strict\'\nconst serverless = require(\'serverless-http\')\nconst MongoClient = require(\'mongodb\').MongoClient\n\nconst api = require(\'./modules/api\')\nconst SecureConfig = require(\'./modules/secureConfig\')\n\nlet dbObject = null\nconst getDBConnection = async (event, context) => {\n try {\n if (dbObject && dbObject.serverConfig.isConnected()) return dbObject\n\n const client = await MongoClient.connect(SecureConfig.mongodb.host, SecureConfig.mongodb.mongoConfig)\n dbObject = client.db(SecureConfig.mongodb.db)\n return dbObject\n } catch(err) {\n throw(err)\n }\n}\n\nmodule.exports.handler = async (event, context) => {\n const db = await getDBConnection()\n const server = serverless(api.default(db));\n try {\n /**\n * Lambda\xe2\x80\x99s context object exposes a callbackWaitsForEmptyEventLoop property,\n * that effectively allows a Lambda function to return its result to the caller\n * without requiring that the MongoDB database connection be closed.\n * This allows the Lambda function to reuse a MongoDB connection across calls.\n */\n context.callbackWaitsForEmptyEventLoop = false\n\n return await server(event, context)\n } catch (error) {\n console.error(\'Lambda handler root error.\')\n throw error\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n您可以在这里找到更多详细信息: https://www.mongodb.com/blog/post/optimizing-aws-lambda-performance-with-mongodb-atlas-and-nodejs
\n 归档时间: |
|
查看次数: |
3481 次 |
最近记录: |