使用无服务器框架(AWS Lambda/Gateway),Express,Mongoose/MongoDB Atlas的应用频繁超时 [英] Frequent timeout with app using Serverless Framework (AWS Lambda/Gateway), Express, Mongoose/MongoDB Atlas
问题描述
触发警告:初学者问题.
我使用Express和Mongoose以及MongoDB Atlas数据库构建了一个api.
在大多数情况下,它可以正常工作,但经常会出现超时错误.这似乎是随机发生的,涉及所有路线,等等.确切地说,我得到了:
`502 Internal server error via POSTMAN`
在无服务器仪表板中,我得到:
invocation
time invoked 1 day ago, mar 08 at 1:38pm
fatal error Function execution duration going to exceeded configured timeout limit.
cold start
duration 48.9 s
memory used na
request
endpoint /{proxy+}
method POST
status 502
message Internal server error
latency 27 ms
和跨度&日志:
我使用本教程包装了我的express应用程序,以使其通过无服务器框架进行部署: https://mongoosejs.com/docs/lambda.html),并阅读其他教程,但是我不应该太适应我的情况.
谢谢您的帮助
该问题可能是由于您打开的数据库连接引起的.建立此连接后,对callback
的任何调用都不会返回给客户端,并且您的函数将超时.
您需要将context.callbackWaitsForEmptyEventLoop
设置为false
.
这是 docs 中的说明:
callbackWaitsForEmptyEventLoop –设置为false可在执行回调时立即发送响应,而不是等待Node.js事件循环为空.如果为假,则在下一次调用期间,任何未完成的事件将继续运行.
使用serverless-http
,您可以设置选项很容易在您的server.js
文件中:
const sls = require('serverless-http')
const app = require('./app')
module.exports.run = sls(app, { callbackWaitsForEmptyEventLoop: false })
Trigger warning : Beginner question.
I built an api using Express and Mongoose with a MongoDB Atlas DB.
Most of the time, it works normally, but often I get timeout errors. This seems to happen very randomly and concerns all routes, etc... Precisely, I get :
`502 Internal server error via POSTMAN`
and in the Serverless Dashboard, I get :
invocation
time invoked 1 day ago, mar 08 at 1:38pm
fatal error Function execution duration going to exceeded configured timeout limit.
cold start
duration 48.9 s
memory used na
request
endpoint /{proxy+}
method POST
status 502
message Internal server error
latency 27 ms
and span & log :
I used this tutorial to wrap my express app to deploy it with serverless framework : https://dev.to/adnanrahic/a-crash-course-on-serverless-apis-with-express-and-mongodb-193k
Serverless.yml file :
service: serviceName
app: appName
org: orgName
provider:
name: aws
runtime: nodejs12.x
stage: ${env:NODE_ENV}
region: eu-central-1
environment:
NODE_ENV: ${env:NODE_ENV}
DB: ${env:DB}
functions:
app:
handler: server.run
events:
- http:
path: /
method: ANY
cors: true
- http:
path: /{proxy+}
method: ANY
cors: true
plugins:
- serverless-offline # Utiliser pour tester localement
- serverless-dotenv-plugin
server.js file :
const sls = require('serverless-http')
const app = require('./app')
module.exports.run = sls(app)
app.js file :
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const newRoutes = require('./routes/file')
const app = express()
app.use(bodyParser.json())
const helmet = require('helmet')
app.use(helmet())
app.options('*', cors())
app.use(cors({ allowedHeaders: 'Content-Type, Authorization' }))
app.use('/new-route', newRoutes)
app.use((error, req, res, next) => {
console.log(error)
const status = error.status || 500
const message = error.message
res.status(status).json({
status: status,
message: message
})
})
// Gère la connexion à la base de donnée :
require('./db')
module.exports = app
and finally db.js file :
const mongoose = require('mongoose')
mongoose
.connect(
process.env.DB, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('connected')
})
.catch(err => console.log(err))
From what I have read, it is related to cold start in Lambda and the way API Gateway handles timeouts (!?). I have read this on mongoose documentation (https://mongoosejs.com/docs/lambda.html), and read also other tutorials, but I don't how exaclty I should adapt it to my situation.
Thank you for your help
The issue is likely due to your open database connection. While this connection is established any calls to callback
won't be returned to the client and your function will timeout.
You need to set context.callbackWaitsForEmptyEventLoop
to false
.
Here is the explanation from the docs:
callbackWaitsForEmptyEventLoop – Set to false to send the response right away when the callback executes, instead of waiting for the Node.js event loop to be empty. If this is false, any outstanding events continue to run during the next invocation.
With serverless-http
you can set this option quite easily within your server.js
file:
const sls = require('serverless-http')
const app = require('./app')
module.exports.run = sls(app, { callbackWaitsForEmptyEventLoop: false })
这篇关于使用无服务器框架(AWS Lambda/Gateway),Express,Mongoose/MongoDB Atlas的应用频繁超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!