Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set maxSockets for all requets to be #1150

Closed
ekelvin opened this issue Oct 13, 2014 · 4 comments
Closed

How to set maxSockets for all requets to be #1150

ekelvin opened this issue Oct 13, 2014 · 4 comments

Comments

@ekelvin
Copy link

ekelvin commented Oct 13, 2014

I tried the request.defaults to set hte maxSocket: 3 but the problem still persists, thousands of requests are sent to the server in parallel :S :(.
here is my try, notice download is with callback

request.defaults({ pool: { maxSockets: 3 } });
var download = function (uri, filename, callback) {        
        request.head(uri, function (err, res, body) {
            if (err) {
                console.error(err);
                return;
            }                    
            if (res.headers['content-type'] == "image/jpeg") {
                request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
            }
            else
                console.error('content-type:' + res.headers['content-type']);
        });        
};
@mikeal
Copy link
Member

mikeal commented Oct 13, 2014

we need to treat the pool option the same way we do headers, creating a full copy of the underlying option.

@ekelvin
Copy link
Author

ekelvin commented Oct 14, 2014

is there a workaround for this problem?

@FredKSchott
Copy link
Contributor

It seems to be working on my end:

var sharedPool = { maxSockets: 5 };
var req = request.defaults({pool: sharedPool});
var r = req.get('http://google.com', function(err, response) { console.log(r.pool) }); 
// Outputs a pool containing agents with maxSocket = 5
sharedPool.foo = 'bar';
var r = req.get('http://google.com', function(err, response) { console.log(r.pool) }); 
// Outputs a pool containing agents with maxSocket = 5 and with property foo='bar'

The first output tests that maxSockets is properly set. The second tests that all requests are using the same, shared pool so that maxSockets is actually having an effect. (These were all done in the repl with enough time for the first request to come back before firing the second one).

@ekelvin request.defaults() doesn't change the actual request module, it returns a new, wrapped module for you to use. Try this:

var requestWithDefaults = request.defaults({ pool: { maxSockets: 3 } });
// and then...
requestWithDefaults.head(uri, function (err, res, body) {

@nylen
Copy link
Member

nylen commented Oct 17, 2014

As @FredKSchott said, do request = request.defaults(...) instead of just request.defaults(...), that seems to be the real issue here.

@nylen nylen closed this as completed Oct 17, 2014
nylen added a commit to nylen/request that referenced this issue Nov 7, 2014
This has come up several times recently, for example request#1150 and request#1249.
nylen added a commit to nylen/request that referenced this issue Nov 7, 2014
This has come up several times recently, for example request#1150 and request#1249.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants