简体   繁体   中英

NodeJs: In Case when maxsockets are limited to 10, how to limit request queue in globalAgent

There is a node server hosted and from application we also make a request to some external api over http. This external service can process 10request/sec. Application is behind Nginx which has timeout 30 secs. Now let say we put load of 10k request on nodejs app server. Since we do have dependency on external api which can process max 10*30 request in 30 secs. Only 300 request will be served and remaining will be terminated by Nginx. But still this 10k request got queued into https.globalAgent.requests queue and keep running. There is no way to specify sockettimeout or limit the size of the request queue. Further call to application will eventually be queued up for external service and later will be terminated by Nginx.

So questions are : Is there any way we can set socketTimeOut? Is there any way we can limit the size of the queue?. Any workaround ?

Sample Code

var http = require('http');
var https = require('https');
var Q = require('q');
var file = require('fs');
var request = require('request');
http.globalAgent.maxSockets = 10;
https.globalAgent.maxSockets = 10;
https.globalAgent.keepAlive=true;


http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});

    var st = new Date();
    var rr = request(
        {url:'https://amazon.com',timeout:100000,time:true},
        function(err,resp,body){
            var et = new Date();
            // console.log( resp && resp.timings.socket,st-et,err);
            console.log(https.globalAgent)
            res.end('ok');
        }
    );


}).listen(9898);

I'll suggest you to use a rate limiting library like https://github.com/jhurliman/node-rate-limiter

In case of an exceded limit, you can answer immediately to the requests, with a 429 error code ( https://httpstatuses.com/429 ) or a ko error message or you can wait until you have an available request slot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM