真正快速处理大量请求时如何解决“套接字挂断错误”

时间:2019-04-11 06:52:05

标签: javascript node.js express request

我有一个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 

环境详细信息: 节点版本-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');
  });
};

请帮助我解决此问题。

1 个答案:

答案 0 :(得分:0)

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

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

还有一件要记住的事,由于“自然”的网络原因(例如,网络连接不稳定,网络繁忙,服务器繁忙导致峰值),请求可能会失败,因此在放弃之前,您应始终至少重试一次请求。