recovery
Recover from a network failure using randomized exponential backoff
Last updated 9 years ago by 3rdeden .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install recovery 
SYNC missed versions from official npm registry.

Recovery

Made by unshiftVersion npmBuild StatusDependenciesCoverage StatusIRC channel

Recovery provides randomized exponential back off for reconnection attempts. It allows you to recover the connection in the most optimal way (for both server and client). The exponential back off is randomized to prevent a DDOS like attack on your server when it's restarted, spreading the reconnection attempts instead of having all your connections attempt to reconnect at exactly the same time.

Features

  • Reconnection and progress events.
  • Randomized exponential back off.
  • Reconnection timeouts.
  • Browserify compatible.

The code base of this module was originally written for Primus but has been extracted as separate module. It has been thoroughly tested and it's written with love <3

Installation

As this module can be used with node.js and browserify it's released in the npm registry and can be installed using:

npm install --save recovery

Events

As mentioned in the documentation introduction, this library provides various of reconnection and progress events. Events always receive a "status" or progress object as last argument. This object contains useful information about current reconnection progress:

  • attempt: Which reconnection attempt are we currently processing.
  • start: Starting time of reconnection attempt.
  • duration: How long have we taken so far to establish the connection.
  • scheduled: In how many ms do we schedule the next reconnection attempt.

In addition to these values it also contains all the configuration options like retries, min, max etc.

The following events are emitted during the recovery process:

Event Arguments Description
reconnect scheduled status Scheduled a new reconnection attempt.
reconnect status, fn It's time for you to reconnect to the server.
reconnected status Successfully reconnected.
reconnect failed err, status Failed to reconnect and ran out of attempts.
reconnect timeout err, status Failed to reconnect in a timely manner, will retry.

Constructing

In all code examples we assume that you've loaded the library using:

'use strict';

var Recovery = require('recovery');

The module is exported as a constructor. The constructor accepts an optional options object which allows you to configure the reconnection procedure. The following options are accepted:

  • max Maximum reconnection delay. Defaults to Infinity.
  • min Minimum reconnection delay. Defaults to 500 ms.
  • retries Maximum amount retries after this we will emit an reconnect failed event. Defaults to 10.
  • reconnect timeout Time you have to reconnect to the server. If it takes longer than the specified value we will emit an reconnect timeout event and schedule another reconnection attempt. Defaults to 30 seconds.
  • factor Exponential back off factor. Defaults to 2.

Options that indicate a time can either be set using a human readable string like 10 seconds, 1 day, 10 ms etc. or a numeric value which represents the time in milliseconds.

var recovery = new Recovery({
  max: '30 seconds',
  min: '100 milliseconds',
  retries: 5
});

Reconnecting

Before every reconnection attempt we emit a reconnect event. You can listen to this event on your assigned event emitter. After the event is emitted we will start a timeout so your attempts have only a limited amount of time to succeed or fail. If the timeout expires we emit a reconnect timeout event and start a whole new reconnection procedure.

If your reconnection attempt is successful call the reconnect.reconnected() method without any arguments. If it failed you can call the method with an error argument. If the operation failed we will automatically schedule a new reconnect attempt. When it's successful we will do some small internal clean up and emit the reconnected event. If all future attempts fail we will eventually emit the reconnect failed event which basically indicates that something horrible is going on.

recovery = new Recovery();

recovery.on('reconnect', function (opts) {
  console.log(opts.attempt);

  reconnectmyconnection(function (err) {
    if (err) return reconnect.reconnected(err);
    reconnect.reconnected();
  });
});

recovery.reconnect();

Alternatively you also call the callback which is provided in the reconnect event which is the same as the reconnected method.

recovery.on('reconnect', function (opts, fn) {
  reconnectmyconnection(fn);
});

To check if a reconnection attempt is already running you can call the reconnecting method which will return a boolean:

if (!recovery.reconnecting()) recovery.reconnect();

And if you wish to cancel the running reconnection attempt you can call the reset method:

if (recovery.reconnecting()) recovery.reset();

License

MIT

Current Tags

  • 0.2.6                                ...           latest (9 years ago)

14 Versions

  • 0.2.6                                ...           9 years ago
  • 0.2.5                                ...           9 years ago
  • 0.2.4                                ...           9 years ago
  • 0.2.3                                ...           10 years ago
  • 0.2.2                                ...           10 years ago
  • 0.2.1                                ...           10 years ago
  • 0.2.0                                ...           10 years ago
  • 0.1.0                                ...           10 years ago
  • 0.0.5                                ...           10 years ago
  • 0.0.4                                ...           10 years ago
  • 0.0.3                                ...           10 years ago
  • 0.0.2                                ...           10 years ago
  • 0.0.1                                ...           10 years ago
  • 0.0.0                                ...           10 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (5)
Dev Dependencies (4)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |