Giter VIP home page Giter VIP logo

pollen's Introduction

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

pollen's People

Contributors

dependabot[bot] avatar shuhei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

00mjk

pollen's Issues

Retry DNS queries

Because of the long polling interval, DNS query errors will result in outdated IP addresses.

Retry DNS queries when they fail.

DNS query timeout

Give up when DNS server doesn't reply or the answer datagram is lost.

nameserver discovery

One thing that pollen could do is to find nameservers to query based on dns configuration, /etc/resolv.conf, this way one would not have to pass it manually.

Could also provide it as a utility function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.