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

machi-fault-tolerance

v1.0.1

Published

Fault tolerance patterns in JavaScript

Downloads

16

Readme

machi fault tolerance

Implement a light weight fault tolerance and high resilience patterns for JavaScript applications.

This package implements the following patterns

  • timeout
  • fallback
  • retry

timeout

The timeout pattern can be implemented as shown in the example below:

const {timeout} = require('machi-fault-tolerance');

const fn = (arg1, arg2, arg3) => { // the function to execute
    // a very long computation
}

const Timeout = timeout(2000); // a timeout of 2000 ms

const result = await Timeout(fn, this, arg1, arg2, arg3) // execute the function fn, in a given context (null or undefined accepted), with the given args within 2 seconds or throw an error

The default timeout value is 1 second.

fallback

The fallback pattern can be implemented as shown in the example below:

const {fallback} = require('machi-fault-tolerance');

const fn = (...args) => { // the function to execute
    // compute or potentially throwing an error
}

const fb = () => {console.log(" fallback executed" )}

const Fallback = fallback(fb, this); // defines a fallback function that can be executed when fn throws an error.

const result = await Fallback(fn, this, arg1, arg2, arg3, argN) // execute the function fn, in a given context (null or undefined accepted), with the given args. If fn throws an error, return results executed by fb.

The fallback constructor optionally accept a third parameter defined which is a function that defines conditional execution of the fallback.

e.g

const {fallback} = require('machi-fault-tolerance');

const fn = (...args) => { // the function to execute
    // compute or potentially throwing an error
}

const fb = () => {console.log(" fallback executed" )}

const Fallback = fallback(fb, this, (error) => error.statusCode === 502); // defines a fallback function that can be executed when an erro with statusCode 502 occurs.

const result = await Fallback(fn, this, arg1, arg2, arg3, argN).

retry

The retry pattern can be used as follows:

  1. minimum configuration
const expected = new Error();
const Retry = retry({maxRetries: 2}); // indicate the number of retries to do before throwing an error. The minimum value is zero. 

const result = await Retry(() => {
    // do something
}, null); // return result of execution of a given funcion or throws an error
  1. conditional retry when error satisfies a given condition
const expected = new Error();
const Retry = retry({
    maxRetries: 2, 
    retryOn: () => {
    return Math.random() * 3 % 2 === 0;
}}
); // indicate the number of retries to do when the "retryOn" function is satisfied before throwing an error. The "retryOn" will receive current error as argument

const result = await Retry(() => {
    // do something
}, null); // return result of execution of a given funcion or throws an error
  1. short circuit retries with "abortOn" function
const expected = new Error();
const Retry = retry({
    maxRetries: 2, 
    abortOn: (error) => {
    return error.statusCode === 403;
}}
); // indicate the number of retries. If an error matches the given condition short circuit retries and throw the corresponding error. 

const result = await Retry(() => {
    // do something
}, null); // return result of execution of a given funcion or throws an error

Author

Manyanda Chitimbo [email protected]