npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

recovery

v0.2.6

Published

Recover from a network failure using randomized exponential backoff

Downloads

1,709

Readme

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