node.js 如何使用请求 js(节点 js 模块)池
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19043355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Request js (Node js Module) pools
提问by Justin Cloud
Can someone explain how to use the request.js pool hash?
有人可以解释如何使用 request.js 池哈希吗?
The github notessay this about pools:
在github上的注释说,这大约池:
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.
pool - 包含这些请求的代理的哈希对象。如果省略,此请求将使用设置为节点默认 maxSockets 的全局池。
pool.maxSockets - 包含池中最大套接字数量的整数。
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:
我有写入 CouchDB 实例的代码(注意问号)。基本上,任何连接到我的 Node 服务器的用户都会相互独立地写入数据库:
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?
什么最适合高吞吐量/性能?
高“maxSockets”数量的缺点是什么?
如何创建一个单独的池而不是全局池来使用?为什么我只想创建一个单独的池?
回答by user568109
The pool option in request uses agent which is same as http.Agentfrom standard http library. See the documentation for http.Agentand see the agentoptions in http.request.
请求中的池选项使用http.Agent与标准 http 库相同的代理。请参阅文档http.Agent,看到了agent期权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 maxSocketsdetermines 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.maxSocketsexplicitly would override the maxSockets property in pool. This option only makes sense if passing pooloption.
该maxSockets决定有多少并发套接字代理可以为每个主机开放的,存在于默认值为5.通常,您将之前设置的代理。pool.maxSockets显式传递将覆盖中的 maxSockets 属性pool。此选项仅在传递pool选项时才有意义。
So different ways to use it :
使用它的不同方式:
- Don't give
agentoption, will beundefinedwill usehttp.globalAgent. The default case. - Give it as false, will disable pooling.
- Provide your own agent, like above example.
- 不要给
agent选择,将是undefined将使用http.globalAgent。默认情况。 - 将其设为 false,将禁用池化。
- 提供您自己的代理,如上例。
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.
maxSockets 属性提供了最大的并发性。它增加了整体吞吐量/性能。缺点是油门效果降低。您无法控制峰值开销。将其设置为大数,就像根本没有池化一样。您会开始收到诸如套接字不可用之类的错误。它不能超过操作系统设置的允许的最大限制。
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.
那么什么最适合高吞吐量/性能?吞吐量存在物理限制。如果达到限制,响应时间将随着连接数的增加而增加。在此之前,您可以继续增加 maxSockets,但在增加之后就无济于事了。
回答by shawmzhu
You should take a look at the forever-agentmodule, which is a wrapper to http.Agent.
您应该查看forever-agent模块,它是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.
一般池是一个散列对象,其中包含多个http代理。它尝试从“保持活动”连接重用创建的套接字。每个主机:端口。例如,您执行了多个请求来托管 www.domain1.com:80 和 www.domain2.com:80,如果任何响应不包含 header Connection: close,它将把套接字放入池中并将其交给待处理的请求。
If no pending requests need this pooled socket, it will be destroyed.
如果没有挂起的请求需要这个池化套接字,它将被销毁。
The maxSocketsmeans 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:
的maxSockets装置,用于单个主机的最大并发插口:端口,默认值是5。我建议将这个值与您的场景一起考虑:
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
maxSocketsvalue of a pool. Notice that it doesn't matter if you set a very high number tomaxSocketswhen the connection is well managed by the origin server via response headerConnection: close.According to those sites that your requests hardly visit, use
pool: falseto disable pool.
根据那些热点站点请求访问,最好创建单独的池。以便新请求可以非常快速地获取空闲套接字。关键是,您需要通过增加
maxSockets池的值来减少对某些站点的待处理请求的数量。请注意,maxSockets当源服务器通过 response header 很好地管理连接时,如果您设置一个非常高的数字并不重要Connection: close。根据您的请求几乎不访问的站点,使用
pool: false禁用池。
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){
});

