博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to use Request js (Node js Module) pools
阅读量:7221 次
发布时间:2019-06-29

本文共 4410 字,大约阅读时间需要 14 分钟。

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

The  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:

1 var request = require('request'); 2  3 request({ 4     //pool:,     //  ?????????????????? 5     'pool.maxSockets' : 100,  //  ?????????????????? 6     'method' : 'PUT', 7     'timeout' : 4000, 8     'strictSSL' : true, 9     'auth' : {10         'username' : myUsername,11         'password' : myPassword12     },13     'headers' : {14         'Content-Type': 'application/json;charset=utf-8',15         'Content-Length': myData.length16     },17     'json' : myData,18     'url': myURL19 }, function (error, response, body){20     if (error == null) {21         log('Success: ' + body);22     }23     else {24         log('Error: ' + error);25     }26 });

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 pool option in request uses agent which is same as http.Agent from standard http library. See the documentation for  and see the agent options in .

Usage

1 pool = new http.Agent(); //Your pool/agent2 http.request({hostname:'localhost', port:80, path:'/', agent:pool});3 request({url:"http://www.google.com", pool:pool });

If you are curious to know what is that you can see it from console.

1 { domain: null,2   _events: { free: [Function] },3   _maxListeners: 10,4   options: {},5   requests: {},6   sockets: {},7   maxSockets: 5,8   createConnection: [Function] }

 

The pool option in request uses agent which is same as http.Agent from standard http library. See the documentation for  and see the agent options in .

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 :

  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.

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.

 

 

转载于:https://www.cnblogs.com/huenchao/p/6235830.html

你可能感兴趣的文章
java中相同名字不同返回类型的方法
查看>>
Rails NameError uninitialized constant class solution
查看>>
Android 获取SDCard中某个目录下图片
查看>>
设置cookies第二天0点过期
查看>>
【转载】NIO客户端序列图
查看>>
poj_2709 贪心算法
查看>>
【程序员眼中的统计学(11)】卡方分布的应用
查看>>
文件夹工具类 - FolderUtils
查看>>
http://blog.csdn.net/huang_xw/article/details/7090173
查看>>
lua学习例子
查看>>
研究:印度气候变暖速度加剧 2040年或面临重灾
查看>>
python爬虫——爬取豆瓣TOP250电影
查看>>
C++与Rust操作裸指针的比较
查看>>
了解webpack-4.0版本(一)
查看>>
如何培养良好的编程风格
查看>>
Netty Channel源码分析
查看>>
基于 HTML5 WebGL 的 3D 机房
查看>>
Java编程——数据库两大神器:索引和锁
查看>>
springMvc学习笔记(2)
查看>>
吐槽Javascript系列二:数组中的splice和slice方法
查看>>