agent.maxFreeSockets 属性在 Node.js 中
agent.maxFreeSockets 属性定义处于空闲状态时保持打开的套接字的数量。它是“http”模块接口的一部分。
语法
agent.maxFreeSockets : number
参数
上述函数可以接受以下参数 -
数字 — 它定义了可以在空闲状态下保持打开的套接字数量。它的默认值设置为 256。
示例
创建一个名为 maxFreeSockets.js 的文件,并复制以下代码段。创建文件后,使用以下命令运行此代码,如以下示例中所示 -
node maxFreeSockets.js
maxFreeSockets.js -
// agent.maxFreeSockets method Demo example // Importing the http & agentkeepalive module const http = require('http'); const agent = require('agentkeepalive'); const keepaliveAgent = new agent({ maxSockets: 100, maxFreeSockets: 10, timeout: 60000, // active socket keepalive for 60 seconds freeSocketTimeout: 30000, // free socket keepalive for 30 seconds }); const options = { host: 'tutorialspoint.com', port: 80, path: '/', method: 'GET', agent: keepaliveAgent, }; console.log("Max free sockets: ",keepaliveAgent.maxFreeSockets); console.log('[%s] agent status changed: %j', Date(), keepaliveAgent.getCurrentStatus());
输出
C:\home
ode>> node maxFreeSockets.js Max free sockets: 10 [Fri Apr 30 2021 12:21:12 GMT+0530 (India Standard Time)] agent status changed: {"createSocketCount":0,"createSocketErrorCount":0,"closeSocketCount":0,"errorS ocketCount":0,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"socket s":{},"requests":{}}
广告