2016-07-15 28 views
0

我已经遇到了这个问题之前几个HTTP交易(如一百个左右的帖子)。今天我试图做7k HTTP请求。这似乎很愚蠢,但它是与目标系统交互的唯一方式。我已经能够做的最好的事情将在大约96%的请求中停顿。它会停下来,永远不会完成最后几个请求。如何在可能出错时可靠地处理数千个HTTP请求?

也许我错误地使用了OiBackoff。它似乎工作正常,但最后3%的GETs将永远不会完成。当日志中的最高重试间隔为40秒时,我让它设置为5分钟,没有请求返回。

我不知道我是否应该像OiBackoff那样一次完成100个请求,以确保它们全部完成。

这里的目标是在最后打一个类似于CFD1234,CFD1236,CFD1238的URL,并将结果(小块xml)推送到数组中。这是代码,最接近我必须工作。也许我需要尝试一个不同的库?我已经尝试过与承诺队列,并不能让它运行。它工作,如果我创建一个函数闭包的数组并依次开火,但它需要永远,比它应该更长。

var cnum, cnums, complete, ld, logger, oibackoff, opt, processHttpGet, request, responses, total, yamljs, _fn, _i, _len; 

    yamljs = require('yamljs'); 

    request = require('request'); 

    oibackoff = require('oibackoff').backoff({ 
    maxTries: 10, 
    delayRatio: 10 
    }); 

    cnums = yamljs.load('./etc/cnumbers.yaml'); 

    responses = []; 

    logger = { 
    debug: console.log, 
    error: console.log 
    }; 

    ld = require('lodash'); 

    cnums = ld.uniq(cnums); 

    logger.debug("cnums len: " + cnums.length); 

    processHttpGet = function(url, opt, cb) { 
    return request.get(url, opt, function(error, resp, body) { 
     if (error != null) { 
     return cb(error, null); 
     } else if (resp.statusCode >= 400) { 
     return cb(resp.statusCode, null); 
     } else { 
     return cb(null, body); 
     } 
    }); 
    }; 

    opt = null; 

    total = cnums.length; 

    complete = 0; 

    _fn = function(CNumber) { 
    var intermediate, url; 
    url = "http://abc:[email protected]/xyz/def/abc.asmx/GetValueByID?ID=" + CNumber; 
    logger.debug("getting " + url); 
    intermediate = (function(_this) { 
     return function(err, tries, delay) { 
     if (err != null) { 
      logger.debug("GET failed for " + url + ":", err); 
      logger.debug("tries: %d, delay: %d", tries, delay); 
     } 
     if (tries > 10) { 
      logger.debug("/n/n Failed max tries."); 
      process.exit(0); 
      return false; 
     } 
     }; 
    })(this); 
    return oibackoff(processHttpGet, url, opt, intermediate, function(error, response) { 
     if (error) { 
     return false; 
     } else { 
     ++complete; 
     responses.push(response); 
     if (complete % 100 === 0) { 
      console.dir({ 
      url: url, 
      response: response 
      }); 
     } 
     logger.debug("success; responses complete: " + complete + ", total: " + total + ", percentage: " + (ld.round(complete/total, 2) * 100) + "%"); 
     if (complete >= total) { 
      logger.debug(responses); 
      return process.exit(0); 
     } 
     } 
    }); 
    }; 
    for (_i = 0, _len = cnums.length; _i < _len; _i++) { 
    cnum = cnums[_i]; 
    _fn(cnum); 
    } 
+0

哇,有什么与downvotes!? – jcollum

+1

也许人们认为你正在努力调皮。我投了btw。 –

+0

@AdamGent答案贴出来,原来很简单,总觉得不错 – jcollum

回答

0

答案是使用Bluebird,Promise.map和并发与退避库。

# coffee 
# exports is an array of buffers 
retry = (require 'u-promised').retry 
Promise = require("bluebird") 

# build array of buffers to post 

Promise.map(exports, (buffer) -> 
    f = -> postToEndpoint(buffer) 
    retry(5, f) # post with up to 5 retries 
, {concurrency: config.export.concurrency}) # 40 for my app 
.then (result) -> 
    c = 0 
    ld.map(result, (x) -> c += x) 
    msg = "Complete. #{c} posts completed." 
    logger.info msg 
.catch (reason) -> 
    logger.error reason