执行延迟HTTP请求的NodeJS队列很奇怪

时间:2017-05-04 13:43:35

标签: node.js http request queue

亲爱的互联网人士, 我开始写一个HTTP队列。我有一个请求类,如果我在没有上下文的情况下使用它,可以工作,但是当我的队列处于活动状态时,我的http请求将无效。我无法弄清楚原因。

var t = new Request('s','s', function(){});
t.perform();

当我在任何文件中执行这样的请求时。有用。但是当我将它与我的队列(index.js,L19到L22)一起使用时,不会执行任何请求。执行了 Request.perform()函数,但HTTP-Request不存在。 抱歉我的英语,我不是本地人^^

index.js

const http    = require('http');
const https   = require('https');
const {Request} = require('./classes/Request');
const queue   = require('./queue.js');

queue.setCallbackFunction(performRequest);
function performRequest(request){
    console.log("2");
    request.perform();
}
var req = new Request('','', function(response,body){
   console.log(JSON.stringify(response) + " :: " + body);
});
queue.add(req);

queue.js

var queue = [];
var ratelimits = [];

module.exports.add = function(request){
    queue.push(request);
    run_queue();
}
module.exports.setCallbackFunction = function(cb){
    call = cb;
}
module.exports.setRateLimits = function(ratelimitings){
    ratelimits = [];
    for(var z in ratelimitings){
        var x = ratelimitings[z];
        var data = {};
        data.max = x[0];
        data.time = x[1];
        data.count = 0;

        ratelimits[x[1]] = data;
    }
}
function run_queue(){
    var q;
    if(queue.length > 0){
        q = run_request(queue[0]);
        while (q == true) {
            queue.shift();
            if(queue.length > 0)
            q = run_request(queue[0]);
        }
    }
}

function run_request(request){
    for(var z in ratelimits){
        var x = ratelimits[z];
        if(x.max <= x.count){
            return false;
        }
    }
    for(var z in ratelimits){
        var x = ratelimits[z];
        if(x.count === 0){
            setTimeout(function(z){
                console.log(JSON.stringify(x));
                ratelimits[z].count = 0;
                run_queue();
            },z,z);
        }
        x.count++;
        //console.log(JSON.stringify(x));
    }
    //DO REQUEST
    console.log("1")
    call(request);
    return true;
}

Request.js

exports.Request = class{
    constructor(host,path,cb){
        this.path = path;
        this.cb = cb;
        this.host = host
    }
    perform(){
        console.log("3");
        var https = require('https');
        var options = {
            host: 'www.example.com',
            path: '/'
        };

        var callback = function(response) {
        //HERE THIS GETS NEVER CALLED BECAUSE OF WHATEVER WHILE ITS IN THE QUEUE
            var str = '';
        //another chunk of data has been recieved, so append it to `str`
            response.on('data', function (chunk) {
                str += chunk;
            });

            //the whole response has been recieved, so we just print it out here
            response.on('end', function () {
                console.log(str);
            });
        }

        https.request(options, callback).end();
    }
}

打印所有3个console.logs,但不会调用Request Callback。

1 个答案:

答案 0 :(得分:0)

问题是由函数run_queue

引起的
function run_queue(){
    var q;
    if(queue.length > 0){
        q = run_request(queue[0]);
        while (q == true) {
            queue.shift();
            if(queue.length > 0)
            q = run_request(queue[0]);
        }
    }
}

成功执行run_request()(发送HTTP请求)后,立即调用queue.shift(),这意味着刚刚添加到队列中的req对象已从{{1}中删除数组和限定为垃圾收集。由于一个HTTP请求/响应通常花费几毫秒,因此很有可能在检索响应之前,queue对象被GCed(销毁)。因此,不会调用任何回调,因为HTTP连接不再存在。

要更改队列但保留GC中的req对象,您需要将其保存在其他位置,例如临时数组(以下代码中的无限循环错误也已修复):

req

请注意以上代码仅用于演示。在生产代码中,您需要管理var tmp = []; function run_queue(){ var q; if(queue.length > 0){ q = run_request(queue[0]); while (q == true) { tmp.push(queue.shift()); if(queue.length > 0) { q = run_request(queue[0]); } else { q = false; } } } } 数组 - 当一个请求完成后,需要从tmp中删除它。否则,tmp数组将继续增长......