gpt4 book ai didi

javascript - 在等待 HTTPS 请求时,事件循环被卡住并且不会响应健康检查 - NodeJS+kubernetes

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:49 24 4
gpt4 key购买 nike

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

快速文字说明:

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

在等待缓慢的服务响应微服务时,事件循环似乎无法响应 kubernetes 进行的运行状况检查。

代码:

worker 模块:

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)

}

}

}

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

API:

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 请求,我尝试将其替换为 Request 以测试这是否与它有关。

基本上,微服务通过 Promise.all 获取 50-150 个请求,在获取相当多的请求后,最终会因为非常慢的答案而消失,因为它不会回答健康检查。

最佳答案

对于任何遇到这个问题并寻找答案的人来说,我最终解决这个问题的方法是大幅增加我的应用程序从 kubernetes 获取的内存。看来这不是事件循环问题,而是性能问题。祝你好运!

关于javascript - 在等待 HTTPS 请求时,事件循环被卡住并且不会响应健康检查 - NodeJS+kubernetes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59837792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com