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

Clarify that defaults() does not modify global defaults #1251

Merged
merged 1 commit into from Nov 10, 2014

Conversation

nylen
Copy link
Member

@nylen nylen commented Nov 7, 2014

This has come up several times recently, for example #1150 and #1249.

This has come up several times recently, for example request#1150 and request#1249.
@anklos
Copy link

anklos commented Nov 10, 2014

@nylen I wonder if we use defaults() to control socket pool, in what circumstances we should use option pool: { maxSockets: XXX}?

@nylen
Copy link
Member Author

nylen commented Nov 10, 2014

If I understand you correctly, I think we cover this adequately in the readme:

  • A maxSockets property can also be provided on the pool object to set the max number of sockets for all agents created (ex: pool: {maxSockets: Infinity}).
  • Note that if you are sending multiple requests in a loop and creating
    multiple new pool objects, maxSockets will not work as intended. To
    work around this, either use request.defaults
    with your pool options or create the pool object with the maxSockets
    property outside of the loop.

nylen added a commit that referenced this pull request Nov 10, 2014
Clarify that defaults() does not modify global defaults
@nylen nylen merged commit f2abdc9 into request:master Nov 10, 2014
@anklos
Copy link

anklos commented Nov 10, 2014

Thanks @nylen

Could you give an example how to create a pool object with maxSockets setting and how to use it with request, is it something like request.pool = myPool?

Please excuse my little knowledge on these things.

@nylen
Copy link
Member Author

nylen commented Nov 10, 2014

We already did it at #1250 (comment):

unirest.request = unirest.request.defaults({ pool : { maxSockets : 5 } });

Or:

// As the documentation says, this won't work inside a loop.
request({ pool : { maxSockets : 5 }, ... }, function(err, res, body) { ... });

@anklos
Copy link

anklos commented Nov 10, 2014

that won't work inside a loop, how about the example I put in #1249 ? Is it also kind of a loop, which runs request({ pool : { maxSockets : 5 }, ... }, function(err, res, body) { ... }); many times as requests come in?

If it does not work in that way, what's the use case of request({ pool : { maxSockets : 5 }, ... }, function(err, res, body) { ... });? Sends one request to an endpoint which could create multiple socket connections?

Thanks.

@nylen
Copy link
Member Author

nylen commented Nov 10, 2014

With this change (remove the - lines, add the + lines) your example should work as you intend:

## client.coffee

 request = require 'request'

+poolOptions =
+  maxSockets: 5

 exports.send = (req, res, next) ->
   options =
     url: "https://google.com.au"
     strictSSL: true
     secureProtocol: true
-    pool:
-      maxSockets: 5
+    pool: poolOptions

   request options, (err, response, body) ->
     res.send 200, body

@anklos
Copy link

anklos commented Nov 10, 2014

how come I missed the second part.. should not reply during the meeting :)

Note that if you are sending multiple requests in a loop and creating multiple new pool objects, 
maxSockets will not work as intended. To work around this, either use request.defaults with your pool 
options or create the pool object with the maxSockets property outside of the loop.

Thanks a lot for your nice help!!

@fritx
Copy link
Contributor

fritx commented Jul 13, 2017

What if I need to global setting some options, such as strictSSL: false?

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

Successfully merging this pull request may close these issues.

None yet

3 participants