Dear helpful people in the internet, i started to write an HTTP Queue. I have a Request Class, which works if I use it without context, but with my queue active, my http requests wont work. I can't figure out why.
var t = new Request('s','s', function(){});
t.perform();
When i perform the request like this in any file. It works. But when i use it with my queue (index.js,L19 to L22) no request is performed. The function Request.perform() were executed, but the HTTP-Request isn't there. Sorry for my english, I'm not native ^^
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();
}
}
All 3 console.logs get printed, but the Request Callback Never gets called.
via Like Blender
No comments:
Post a Comment