re-pair

use

reconnect robustness

npm install re-pair
3 downloads in the last week
7 downloads in the last month

NAME

re-pair(3)

SYNOPSIS

uber simple reconnect logic for tls or tcp.

MOTIVATION

The process of reconnecting a client should be smarter than just re-trying the same server repeatedly. A client should attempt to round-robin connection attempts to a series of servers running on different subnets (or at least in different data centers) and exponentially back off from connections that fail.

EXAMPLES

Connect to a server

var restream = require('restream');

var options = {
  reconnectTime: 128, // time before attempting to reconnect
  connectTime: 1024,  // max time spent attempting to connect
  servers: [
    { port: 9960, host: '192.0.0.2', },
    { port: 9968, host: '187.0.0.1', },
    { port: 9020, host: '123.0.0.3', },
    { port: 9967, host: '111.1.1.1' }
  ]
}

var client = restream.connect(options)

client.on('connect', function(connection) {
  // ...connected, event may occur many times.
})

client.on('error', function(err, port, host) {
  // ...could not connect to a specific server.
})

client.on('fail', function() {
  // ...could not connect to any servers.
})

API

connect([options])

The options object also accepts the regular options that are accepted by the tls.connect andtcp.connect` methods. Method returns an event emitter.

[options] { protocol: <Object> }

Specify if something other than tcp should be used.

[options] { reconnectTime: <Number> }

An optional number to determine how long before attempting the next reconnection. Defaults to 3e4. Note that this number is multiplied by the number of failures to connect.

[options] { connectTime: <Number> }

An optional number to determine how long before a simple connection attempt should be timed out.

[options] { servers: <Object> }

An array that contains a list of servers. Each object in the array should contain a port and host address. When a connection can't be made to a server, the next server in the array is selected for the reconnect attempt.

[options] { maxCycles: <Number> }

How many times to cycle though the start list before calling the onFail method.

npm loves you