Skip to content
DNS polling for HTTP Keep-Alive of Node.js HTTP clients
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
__tests__
.gitignore
.npmignore
LICENSE
README.md
example.js
index.js
jest.config.js
package.json
poller.js
yarn.lock

README.md

pollen

DNS polling for HTTP Keep-Alive of Node.js HTTP clients

CircleCI codecov npm version

Why?

  • Support server-side traffic switch using weighted DNS routing
  • Eliminate latency of making new connections (DNS query, TCP & TLS handshakes)
  • Stay resilient to DNS query failures

To achieve good performance, we want to keep Keep-Alive connections as much as possible. On the over hand, DNS-based traffic switch doesn't allow us to keep connections forever because existing persistent connections can become stale when DNS records change.

Solution

This package polls DNS records and keep Keep-Alive connections using agentkeepalive as long as DNS records stay same. When DNS records change, it creates new connections with already retrieved IP addresses without making new DNS queries.

Even when DNS records change, existing connections are not immediately terminated. We can keep them for the next DNS record change (for example, DNS records can go back and forth with weighted DNS routing) with freeSocketTimeout option of agentkeepalive.

Install

npm install -S @shuhei/pollen
# or
yarn add @shuhei/pollen

Example

const https = require('https');
const { DnsPolling, HttpsAgent } = require('@shuhei/pollen');

const dnsPolling = new DnsPolling({
  interval: 30 * 1000 // 30 seconds by default
});
// Just a thin wrapper of https://github.com/node-modules/agentkeepalive
// It accepts all the options of `agentkeepalive`.
const agent = new HttpsAgent();

const hostname = 'shuheikagawa.com';
const req = https.request({
  hostname,
  path: '/',
  // Make sure to call `getLookup()` for each request!
  lookup: dnsPolling.getLookup(hostname),
  agent,
});

Limitations

  • Only IPv4 is supported.
  • /etc/hosts, etc are not supported because this package internally uses dns.resolve4(). See Node.js documentation for more details.

Development

To run the integration tests with multiple local IP addresses on Mac:

sudo ifconfig lo0 alias 127.0.0.2
You can’t perform that action at this time.