【问题标题】:node.js large number of requests throwing errornode.js大量请求抛出错误
【发布时间】:2015-07-14 05:35:45
【问题描述】:

我的图像下载器脚本遇到了一些问题,所以我有一个带有图像名称的数组(大小约为 5000 个元素),我在它的数组中执行循环,并为每次迭代下载带有请求模块的图像。

一切正常,但只有这样我才有大小不大于 500+- 元素的数组。

如果我运行包含 5000 多个元素的脚本,我会看到许多来自请求模块(错误或欠精细响应对象)的错误信息,最后所有应用程序都可能因 EMPTY FILE ERROR 而失败。我认为有一些异步问题导致 NODE.JS 一次没有处理这么多操作。

也许我可以通过将我的 5000 大小的大型数组除以 300 个项目来解决它,并且不要在前一个块之前的下一个块上迭代(并且不要调用 fetchImage())。或者也许有更好的方法来解决我的问题。?

products.map(function (product) {
        fetchImage(product.imageUrl,'./static/' + product.filename + '_big.', 0, 0);
        return;
    });

function fetchImage(url, localPath, index, iteration) {
    var extensions = ['jpg', 'png', 'jpeg', 'bmp' , ''];

    if (iteration > 2 || index === extensions.length) { // try another extensions
        iteration++;
        if(iteration < 3) {
            setTimeout(function(){
                fetchImage(url, localPath, 0, iteration);
            }, 3000);
        }else{
            console.log('Fetching ' + url + ' failed or no image exists ');
            return;
        }
        return;
    }

    var fileExtension;

    if(extensions[index] === '' ) {
        fileExtension = extensions[0];
    }else{
        fileExtension = extensions[index];
    }

    request.get(url + extensions[index], function(err,response,body) {

        if(err || undefined === response){   // if err try after 3 sec timeout
            setTimeout(function(){
                console.log('Error URL : ' + url + extensions[index]);
                fetchImage(url, localPath, index, iteration);
            }, 3000);
            return;
        }else{
            if(response.statusCode === 200) {
                request(url + extensions[index])                  
                    .on('error', function(err) {
                        console.log("ERRRRRROR " + url + extensions[index] + " " + err);
                        setTimeout(function(){
                            console.log('Error URL : ' + url + extensions[index]);
                            fetchImage(url, localPath, index, iteration);
                        }, 3000);
                        return;
                    })
                    .pipe(fs.createWriteStream(localPath + fileExtension ));// write image to file

                console.log('Successfully downloaded file ' + localPath + fileExtension);
                return;
            }else {
                fetchImage(url, localPath, index + 1, iteration);
            }
        }
    });
};

【问题讨论】:

  • 您看到的确切错误消息是什么?
  • 我收到很多消息,例如错误:连接 ETIMEDOUT
  • 听起来您的网络或服务器的网络无法处理您发送的请求的数量/频率。试着放慢一点。
  • 也许你可以帮助我如何减慢我的请求?
  • @MeetJoeBlack 尝试请求限制模块。 NPM 中可用的很少。

标签: javascript arrays node.js request


【解决方案1】:

在每个请求之间使用 setTimeOut 修复

setTimeout(
            function () {
                fetchImage(imageUrl,'./static/' + filename + '_big.', 0, 0);
            },
                300 * (i + 1) // where they will each progressively wait 300 msec more each
        );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-02
    • 2018-03-18
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    相关资源
    最近更新 更多