gpt4 book ai didi

javascript - 如何在非常快速地发出大量请求时修复 "Socket Hangup Error "

转载 作者:行者123 更新时间:2023-11-30 19:38:53 27 4
gpt4 key购买 nike

我有一个 nodejs 应用程序,可以聚合来自各个网站的内容。发出请求以使用请求流从不同来源异步获取提要。发出请求时,我经常收到套接字挂起错误。

err in accessing the link { Error: socket hang up
at createHangUpError (_http_client.js:331:15)
at TLSSocket.socketOnEnd (_http_client.js:423:23)
at emitNone (events.js:111:20)
at TLSSocket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9) code: 'ECONNRESET' } https://arstechnica.com/?p=1488489

环境细节: Node 版本 - v8.12.0

尝试了相关 SO 帖子中给出的一些建议,但我仍然遇到相同的错误。 NodeJS - What does "socket hang up" actually mean?

import request from 'request';
import FeedParser from 'feedparser';

const extractor = require('unfluff');

export const getFeedsFromSource = function (urlfeed, etag, LastModified, callback) {
console.log(urlfeed, etag, LastModified);
const req = request({
method: 'GET',
url: urlfeed,
headers: {
'If-None-Match': etag,
'If-Modified-Since': LastModified,
Connection: 'keep-alive',
ciphers: 'DES-CBC3-SHA',
},
});
const feedparser = new FeedParser();
const metaData = {};
const htmlData = {};
const feedData = {};
// const pList = null;
req.on('response', function (response) {
const stream = this;
if (response.statusCode === 304) {
console.log('Source not modified: ', urlfeed);
}
if (response.statusCode === 200) {
metaData.etagin = response.headers.etag;
metaData.LastModifiedin = response.headers['last-modified'];
metaData.LastModifiedLocal = response.headers['last-modified'];
stream.pipe(feedparser).end();
}
});
req.on('error', (err) => {
console.log(`getFeed: err.message == ${err.message}`);
callback(err);
});
// req.end();
feedparser.on('readable', function () {
try {
const item = this.read();
if (item !== null) {
request({
method: 'GET',
url: item.link,
}, (err, info) => {
if (!err) {
htmlData.body = info.body;
const parsedData = extractor(htmlData.body, 'en');
feedData.author = [];
feedData.videos = [];
feedData.feedtitle = parsedData.title;
feedData.feedmainpicture = parsedData.image;
feedData.feedsummary = parsedData.description;
feedData.feedmaincontent = parsedData.text;
feedData.author.push(item.author);
if (item.author === null) {
feedData.author = parsedData.author;
}
feedData.feedurl = item.link;
feedData.copyright = item.meta.copyright;
// feedData.videos = parsedData.videos;
feedData.publishedDate = item.pubdate;
if (item.categories.length > 0) {
feedData.categories = item.categories;
feedData.feedtags = item.categories;
} else if (parsedData.keywords !== undefined) {
feedData.categories = parsedData.keywords.split(' ').join('').split(',');
feedData.feedtags = parsedData.keywords.split(' ').join('').split(',');
} else {
feedData.categories = [];
feedData.feedtags = [];
}
metaData.sourcename = item.meta.title;
callback(undefined, feedData, metaData);
} else {
console.log('err in accessing the link', err, item.link);
}
});
}
} catch (err) {
console.log(`getFeed: err.message == ${err.message}`);
}
});
feedparser.on('error', (err) => {
console.log(`getFeed: err.message == ${err.message}`);
});
feedparser.on('end', () => {
console.log('onend');
});
};

请帮我解决这个问题。

最佳答案

生产应用程序中套接字挂起/重置的原因有很多。根据您的描述,我认为原因不是由于请求导致应用程序过载(除非您运行的机器非常慢)。IMO,最有可能的候选人是由于来自同一 ip 的连接太多而受到远程服务器的限制(chrome 打开最多 8 个连接到任何单个服务器,你应该尽量不要超过这个限制,尽管每个服务器都有不同的限制),以解决这个问题您应该执行以下操作之一:

  • 添加主机请求池(基本设置Agent.maxSockets)
  • 使用代理服务(例如 Luminati)将请求分发到多个源 ip(与高并发要求更相关)

还有一点要记住,请求可能会因“自然”​​网络原因而失败(例如,互联网连接不良\不稳定、服务器繁忙高峰),您应该始终在放弃之前至少重试一次请求。

关于javascript - 如何在非常快速地发出大量请求时修复 "Socket Hangup Error ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55626343/

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