Home > Web Front-end > JS Tutorial > agent.maxSockets property in Node.js

agent.maxSockets property in Node.js

王林
Release: 2023-08-24 12:29:09
forward
1333 people have browsed it

Node.js 中的 agent.maxSockets 属性

agent.maxSockets property defines the number of sockets that the agent can open simultaneously for each source. By default, this value is set to infinity. This is also part of the "http" module.

Syntax

agent.maxSockets: number
Copy after login

Parameters

The above function can accept the following parameters-

  • number – This definition Determines the number of concurrent sockets the agent can have. Its default value is set to Infinity.

Example

Create a file named maxSockets.js and copy the following code snippet. After creating the file, run this code using the following command, as shown in the following example:

node maxSockets.js
Copy after login

maxSockets.js

// agent.maxSockets 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.maxSockets);
console.log('[%s] agent status changed: %j', Date(),
keepaliveAgent.getCurrentStatus());
Copy after login

Output

C:\homeode>> node maxSockets.js
Max sockets: 100
[Fri Apr 30 2021 12:28:24 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":{}}
Copy after login

The above is the detailed content of agent.maxSockets property in Node.js. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template