Node.js 中的 agent.maxFreeSockets 属性

该agent.maxFreeSockets属性定义处于空闲状态时保持打开的套接字数。这是“http”模块接口的一部分。

语法

agent.maxFreeSockets : number

参数

上述函数可以接受以下参数 -

  • number  – 这定义了在空闲状态下可以保持打开的套接字数量。其默认值设置为 256。

示例

创建一个具有名称的文件 -maxFreeSockets.js并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示 -

node maxFreeSockets.js

maxFreeSockets.js -

//agent.maxFreeSocketsmethod 演示示例

// Importing the http & agentkeepalive module
const http = require('http');
const agent = require('agentkeepalive');

const keepaliveAgent = new agent({
   maxSockets: 100,
   maxFreeSockets: 10,
   timeout: 60000, // 活动套接字保活 60 秒
   freeSocketTimeout: 30000, // 空闲套接字保活 30 秒
});

const options = {
   host: 'nhooo.com',
   port: 80,
   path: '/',
   method: 'GET',
   agent: keepaliveAgent,
};
console.log("最大空闲插槽数: ",keepaliveAgent.maxFreeSockets);
console.log('[%s] agent status changed: %j', Date(),
keepaliveAgent.getCurrentStatus());
输出结果
C:\home\node>> node maxFreeSockets.js
最大空闲插槽数: 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":{}}