Can someone explain how to use the request.js pool hash?
The github notes say this about pools:
pool - A hash object containing the agents for these requests. If omitted this request will use the global pool which is set to node's default maxSockets.
pool.maxSockets - Integer containing the maximum amount of sockets in the pool.
I have this code for writing to a CouchDB instance (note the question marks). Basically, any user who connects to my Node server will write to the DB independent of each other:
var request = require('request'); request({ //pool:, // ?????????????????? 'pool.maxSockets' : 100, // ?????????????????? 'method' : 'PUT', 'timeout' : 4000, 'strictSSL' : true, 'auth' : { 'username' : myUsername, 'password' : myPassword }, 'headers' : { 'Content-Type': 'application/json;charset=utf-8', 'Content-Length': myData.length }, 'json' : myData, 'url': myURL }, function (error, response, body){ if (error == null) { log('Success: ' + body); } else { log('Error: ' + error); } });
What's best for high throughput/performance?
What are the drawbacks of a high 'maxSockets' number?
How do I create a separate pool to use instead of the global pool? Why do I only want to create a separate pool?
The request module is used to make HTTP calls. It is the simplest way of making HTTP calls in node. js using this request module. It follows redirects by default.
var express = require('express'); var app = express(); app. get("/page/:id",function(request, response){ var id = request.params.id; // do something with id // send a response to user based on id var obj = { id : id, Content : "content " +id }; response. writeHead(200, {"Content-Type": "application/json"}); response.
The pool option in request uses agent which is same as http.Agent
from standard http library. See the documentation for http.Agent and see the agent
options in http.request.
Usage
pool = new http.Agent(); //Your pool/agent http.request({hostname:'localhost', port:80, path:'/', agent:pool}); request({url:"http://www.google.com", pool:pool });
If you are curious to know what is that you can see it from console.
{ domain: null, _events: { free: [Function] }, _maxListeners: 10, options: {}, requests: {}, sockets: {}, maxSockets: 5, createConnection: [Function] }
The maxSockets
determines how many concurrent sockets the agent can have open per host, is present in an agent by default with value 5. Typically you would set it before. Passing pool.maxSockets
explicitly would override the maxSockets property in pool
. This option only makes sense if passing pool
option.
So different ways to use it :
agent
option, will be undefined
will use http.globalAgent
. The default case.Answering your questions in reverse.
Pool is meant to keep certain number of sockets to be used by the program. Firstly the sockets are reused for different requests. So it reduces overhead of creating new sockets. Secondly it uses fewer sockets for requests, but consistently. It will not take up all sockets available. Thirdly it maintains queue of requests. So there is waiting time implied.
Pool acts like both a cache and a throttle. The throttle effect will be more visible if you have more requests and lesser sockets. When using global pool it may limit functioning of two different clients, there are no guarantees on waiting time. Having separate pool for them will be fairer to both (think if one requests more than other).
The maxSockets property gives maximum concurrency possible. It increases the overall throughput/performance. Drawback is throttle effect is reduced. You cannot control peak overhead. Setting it to large number, will be like no pooling at all. You would start getting errors like socket not available. It cannot be more than the allowed maximum limit set by the OS.
So what is best for high throughput/performance? There is a physical limit in throughput. If you reach the limit, response time will increase with number of connections. You can keep increasing maxSockets till then, but after that increasing it will not help.
You should take a look at the forever-agent module, which is a wrapper to http.Agent.
Generally the pool is a hash object that contains a number of http agent. it tries to reuse created sockets from "keep-alive" connection. per host:port. For example, you performed several requests to host www.domain1.com:80 and www.domain2.com:80, if any of response contains no header Connection: close
, it will put the socket in pool and give it to pending requests.
If no pending requests need this pooled socket, it will be destroyed.
The maxSockets
means the max concurrent sockets for a single host:port, the default value is 5. I would suggest thinking of this value with your scenario together:
According to those hot sites requests visit, you'd better create separate pool. so that new requests can pick up idle sockets very fast. the point is, you need to reduce the number of pending requests to certain sites by increasing maxSockets
value of a pool. Notice that it doesn't matter if you set a very high number to maxSockets
when the connection is well managed by the origin server via response header Connection: close
.
According to those sites that your requests hardly visit, use pool: false
to disable pool.
You can use this way to specify separate pool for your request:
// create a separate socket pool with 10 concurrent sockets as max value. var separateReqPool = {maxSockets: 10}; var request = require('request'); request({url: 'http://localhost:8080/', pool: separateReqPool}, function(e, resp){ });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With