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":{}}

更新於: 2021-05-20

386 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告