如何使用请求js(节点js模块)池 [英] How to use Request js (Node js Module) pools

查看:189
本文介绍了如何使用请求js(节点js模块)池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释如何使用request.js池哈希吗?

Can someone explain how to use the request.js pool hash?

github笔记谈到有关泳池的内容:

The github notes say this about pools:

pool-包含这些请求的代理的哈希对象.如果省略此 请求将使用设置为节点默认maxSockets的全局池.

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-包含池中最大套接字数量的整数.

pool.maxSockets - Integer containing the maximum amount of sockets in the pool.

我有这段代码用于写入CouchDB实例(注意问号).基本上,任何连接到我的Node服务器的用户都将彼此独立地写入数据库:

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);
    }
});

什么是高吞吐量/高性能的最佳选择?
'maxSockets'数高的缺点是什么?
如何创建单独的池而不是全局池?为什么我只想创建一个单独的池?

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?

推荐答案

请求中的pool选项使用的代理与标准http库中的http.Agent相同.请参阅 http.Agent 的文档,并在http.request .

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.

用法

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] }

maxSockets确定代理可以为每个主机打开多少并发套接字,默认情况下,代理中代理的存在值为5.通常,您需要先设置它.显式传递pool.maxSockets将覆盖pool中的maxSockets属性.仅当传递pool选项时,此选项才有意义.

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.

使用方式不同:

  1. 不提供agent选项,将为undefined将使用http.globalAgent.默认情况.
  2. 将其设置为false,将禁用池化.
  3. 像上面的示例一样提供您自己的代理.
  1. Don't give agent option, will be undefined will use http.globalAgent. The default case.
  2. Give it as false, will disable pooling.
  3. Provide your own agent, like above example.

反向回答您的问题.

池旨在保留程序要使用的一定数量的套接字.首先,套接字被重用于不同的请求.因此,它减少了创建新套接字的开销.其次,它使用更少的套接字来进行请求,但始终如一.它不会占用所有可用的插槽.第三,它维护请求队列.因此,暗示了等待时间.

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).

maxSockets属性可提供最大的并发性.它提高了整体吞吐量/性能.缺点是节气门效果降低.您无法控制峰值开销.将其设置为大数,就像根本没有池化一样.您将开始遇到诸如套接字不可用之类的错误.不能超过操作系统设置的允许的最大限制.

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.

这篇关于如何使用请求js(节点js模块)池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆