Agent pool that rotates keepalive agents for traffic switching via DNS
npm install agent-poolkeepAlive in Node.js to reuse the connections. The kept alive connections does not incur DNS + TCP + TLS overhead. An Agent also takes care of all low-level socket pooling(adding/removing sockets) capabilities that your application doesn't need to worry about.
weighted DNS records, you won't see any redirection of the traffic to the new stack because the connection is reused as long as there are requests to be processed. To force clients to switch, we have to disable the load balancer of the old stack or delete it completely.
sh
yarn add agent-pool
`
Usage
`js
const AgentPool = require("agent-pool");
const { HttpsAgent } = require("agentkeepalive");
const httpsPool = new AgentPool({
maxAgents: 5,
agentType: HttpsAgent,
destroyTime: 1000 * 60 // 1 min
});
const agent = httpsPool.getAgent(); // returns the active agent
// Use it in your request lib
const https = require("https");
const { URL } = require("url");
https.request(
Object.assign(new URL("https://example.com");, {
agent
})
);
`
API
$3
Instantiate the Agentpool instance with options and agentOptions that are passed to the underlying HTTP/HTTPS Agent.
- options {Object} - Configurable options on the agent pool
- agentType - An Agent class that is responsible for managing connection pooling. (default: agentkeepalive HttpAgent)
- maxAgents - The maximum number of agents that are kept in the pool at a given point of time (default: 3)
- destroyTime - The minimum time required for the new agents to become active and start serving requests (default: 1 minute)
- logger - Custom logger that is compatible with console API to log the agent activity. You can use pino (default: console)
- agentOptions` {Object} - Configurable options that are passed to the underlying Agent - Check agentkeepalive