在等待HTTPS请求时,事件循环卡住,无法回答健康检查 - NodeJS+kubernetes



我真的迷失了,我尝试了很多事情,比如改变超时,使用不同的库等,我希望有人能明白为什么会发生这种情况的实际代码给我带来麻烦。 我会尽量简洁。

快速文本说明:

工作人员从队列中选取作业,使用模块,并将所有必需的信息传输到该模块。 然后,该模块将所有请求的 Promise.all 发送到另一个带有 axios 的微服务。 然后,此微服务向我无法控制的服务发出另一个 https 请求,这通常需要很长时间才能回答(1-10 分钟(。 服务应返回一个文档,然后微服务将返回到辅助角色使用的模块。

似乎在等待慢速服务响应微服务时,事件循环无法回答 kubernetes 所做的健康检查。

法典:

工人模块:

const axios = require('axios');
const { getVariableValue } = require("redacted");
const logger = require('redacted');
const https = require('https')
module.exports = webApiOptiDocService = {
async getDocuments(docIDs, transactionId, timeStamp) {
try {
var documents = [];
await Promise.all(docIDs.map(async (docID) => {
var document = await axios.post(getVariableValue("GET_DOCUMENT_SERVICE_URL"), docID, {
httpsAgent: new https.Agent({
rejectUnauthorized: false,
keepAlive: true
}),
auth: {
username: getVariableValue("WEBAPI_OPTIDOCS_SERVICE_USERNAME"),
password: getVariableValue("WEBAPI_OPTIDOCS_SERVICE_PASSWORD")
},
headers: {
"x-global-transaction-id": transactionId,
"timeStamp": timeStamp
}
});
documents.push(document.data.content);
}));
return documents
}
catch (err) {
const responseData = err.response ? err.response.data : err.message
throw Error(responseData)
}
}
}

这是然后获取这些请求的微服务:

应用程序接口:

const express = require('express');
const router = express.Router();
const logger = require('redacted');
const getDocuemntService = require('./getDocumentService')
var clone = require('clone')
module.exports = router.post('/getDocument', async (req, res, next) => {
try {
var transactionId = req.headers["x-global-transaction-id"]
var timeStamp = req.headers["timestamp"]
var document = await getDocuemntService(req.body, transactionId, timeStamp);
var cloneDocument = clone(document)
res.status(200);
res.json({
statusDesc: "Success",
status: true,
content: cloneDocument
});
}
catch (err) {
res.status(500).send("stack: " + err.stack + "err: " + err.message + " fromgetdocument")
}
});

这是它随后使用的 getDocument.js 模块:

const axios = require('axios');
const { getVariableValue } = require("redacted");
const logger = require('redacted');
const https = require('https')
const fileType = require('file-type')
var request = require('request-promise-native')
module.exports = async (docID, transactionId, timeStamp) => {
try {

var startTime = Date.now();

const documentBeforeParse = await request.get(getVariableValue("WEBAPI_OPTIDOCS_SERVICE_URL_GET") + docID.docID, {
strictSSL: false,
headers: {
'x-global-transaction-id': transactionId
},
timeout: Infinity
}).auth(getVariableValue("WEBAPI_OPTIDOCS_SERVICE_USERNAME"), getVariableValue("WEBAPI_OPTIDOCS_SERVICE_PASSWORD"))
const parsedDocument = JSON.parse(documentBeforeParse)
var document = {
data: {
mimeType: parsedDocument.mimeType,
fileName: "",
content: parsedDocument.content
}
}
// const document = await axios.get(getVariableValue("WEBAPI_OPTIDOCS_SERVICE_URL_GET") + docID.docID, {
//     maxContentLength: 524288000, //500 MB in Bytes
//     maxBodyLength: 524288000, //500 MB in Bytes
//     method: 'get',
//     httpsAgent: new https.Agent({
//         rejectUnauthorized: false
//     }),
//     auth: {
//         username: getVariableValue("WEBAPI_OPTIDOCS_SERVICE_USERNAME"),
//         password: getVariableValue("WEBAPI_OPTIDOCS_SERVICE_PASSWORD")
//     },
//     headers: {
//         'x-global-transaction-id': transactionId
//     }
// });
if (document.data.mimeType == "") {
if (recoverMimeType(document.data.content)) {
document.data.mimeType = recoverMimeType(document.data.content).ext
}
else {
throw Error("Missing mime type can not be recovered.")
}
}
var fixedMimeType = fixMimeType(document.data.mimeType);
document.data.fileName = docID.docPartsPrefixName + fixedMimeType
var returnDocument = {
fileName: document.data.fileName,
content: document.data.content,
mimeType: document.data.mimeType
}
return returnDocument;
}
catch (error) {
throw Error(error);
}
}
function fixMimeType(mimeType) {
return "." + mimeType
}
function recoverMimeType(content) {
return fileType.fromBuffer(Buffer.from(content[0], "base64"))
}

我删除了所有记录器,但保留了注释掉的 axios 请求,我尝试将其替换为请求以测试这是否与它有关。

基本上,微服务通过 Promise.all 获得 50-150 个请求,并且在获取大量请求后,最终会在非常慢的答案上消失,因为它不会回答运行状况检查。

对于任何偶然发现并寻找答案的人来说,我最终所做的修复是大大增加我的应用程序从 kubernetes 获得的内存。 似乎这不是事件循环问题,而是性能问题。 祝你好运!

在你正在使用的工人模块中,你正在使用 等待 并承诺所有,

承诺全部异步工作,await 用于阻止事件循环的同步代码。

您可以使用 await 关键字调用函数,该函数返回同步工作的承诺

用于异步/等待使用的 PFA

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

最新更新