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

circuit-breaker-monad

v1.0.1

Published

Circuit Breaker pattern as a monad

Downloads

173

Readme

Circuit Breaker pattern as a monad

npm Build Status

Part of fp-ts ecosystem.

TypeScript implementation of Circuit Breaker pattern. Adaptation of Glue.CircuitBreaker module from Haskell.

You can read a bit more abou the pattern and this implementation in my article.

Usage example

First of all, you need to install the package:

npm install circuit-breaker-monad

Then import the main circuitBreaker function:

import { circuitBreaker } from 'circuit-breaker-monad/lib';

This function returns a Reader, which, given the corresponding BreakerOptions, creates an enhanced fetcher – a function which takes any Lazy<Promise<T>> instance and returns a tuple of IORef<BreakerStatus> and TaskEither<Error, T>. The first element of this tuple is current circuit breaker status, implemented via IORef (mutable reference in the IO monad), and the second element of the tuple is ready-to-be-called TaskEither.

Let's look at the usage example:

import { fold } from 'fp-ts/lib/Either';
import { IORef } from 'fp-ts/lib/IORef';

import { circuitBreaker, defaultBreakerOptions } from 'circuit-breaker-monad/lib';
import { BreakerClosed } from 'circuit-breaker-monad/lib/types';

const fetcher = circuitBreaker<Response>()(defaultBreakerOptions);

const main = async () => {
  const request = () => fetch('http://my-domain.com/my-data.json').then((res) => res.json());
  const breakerState = new IORef(new BreakerClosed(0)); // initial breaker state
  const [result, ref] = fetcher({ request, breakerState });
  const response = await result();

  fold(
    (e: Error) => { },
    (myData) => { },
  )(response);

  // ref :: BreakerClosed { errorCount: 0 }
  // result :: TaskEither<Error, Response>
  // response :: Either<Error, Response>
  // myData :: TMyJsonData
};

The ref variable is resulting circuit breaker status, which can be passed to the next call, so you take the full control over what's going on inside the circuit breaker:

const [ref, result] = fetcher(promise);
const myData1 = await result();
const [, result2] = fetcher(promise, ref);
const myData2 = await result2();
// here `ref` may be 'Open' if the second call to the service has failed

Configuration

Circuit breaker may be configured by passing these parameters to the Reader:

  • maxBreakerFailures - how many times the underlying service must fail in the given window before the circuit opens;
  • resetTimeoutSecs - the window of time in which the underlying service must fail for the circuit to open, seconds;
  • breakerDescription - description that is attached to the failure so as to identify the particular circuit.

Default options are:

export const defaultBreakerOptions: BreakerOptions = {
  maxBreakerFailures: 3,
  resetTimeoutSecs: 60,
  breakerDescription: 'Circuit breaker is closed',
};