All Projects → YBogomolov → Circuit Breaker Monad

YBogomolov / Circuit Breaker Monad

Licence: mit
Circuit Breaker pattern as a monad

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Circuit Breaker Monad

Samples
Steeltoe samples and reference application collection
Stars: ✭ 586 (+1026.92%)
Mutual labels:  circuit-breaker
Luffy
Luffy is a simple resilience and transient-fault handling library
Stars: ✭ 19 (-63.46%)
Mutual labels:  circuit-breaker
Semian
🐒 Resiliency toolkit for Ruby for failing fast
Stars: ✭ 976 (+1776.92%)
Mutual labels:  circuit-breaker
Istio
Connect, secure, control, and observe services.
Stars: ✭ 28,970 (+55611.54%)
Mutual labels:  circuit-breaker
Guard
NOT MAINTAINED! A generic high performance circuit breaker & proxy server written in Go
Stars: ✭ 745 (+1332.69%)
Mutual labels:  circuit-breaker
Purify
Functional programming library for TypeScript - https://gigobyte.github.io/purify/
Stars: ✭ 843 (+1521.15%)
Mutual labels:  monad
Learn Fp
learn-by-doing course/tutorial for functional programming on scala
Stars: ✭ 548 (+953.85%)
Mutual labels:  monad
Cockatiel
A resilience and transient-fault-handling library that allows developers to express policies such as Backoff, Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback. Inspired by .NET Polly.
Stars: ✭ 993 (+1809.62%)
Mutual labels:  circuit-breaker
Fp Core.rs
A library for functional programming in Rust
Stars: ✭ 772 (+1384.62%)
Mutual labels:  monad
Perhaps
A monad, perhaps.
Stars: ✭ 35 (-32.69%)
Mutual labels:  monad
Static Land
Specification for common algebraic structures in JavaScript based on Fantasy Land
Stars: ✭ 699 (+1244.23%)
Mutual labels:  monad
Lambda
Functional patterns for Java
Stars: ✭ 737 (+1317.31%)
Mutual labels:  monad
Witchcraft
Monads and other dark magic for Elixir
Stars: ✭ 864 (+1561.54%)
Mutual labels:  monad
Steeltoe
Steeltoe .NET Core Components: CircuitBreaker, Configuration, Connectors, Discovery, Logging, Management, and Security
Stars: ✭ 612 (+1076.92%)
Mutual labels:  circuit-breaker
Purefun
Functional Programming library for Java
Stars: ✭ 37 (-28.85%)
Mutual labels:  monad
Category Theory
An axiom-free formalization of category theory in Coq for personal study and practical work
Stars: ✭ 562 (+980.77%)
Mutual labels:  monad
Sample Camel Spring Boot
three samples in different branches that illustrates usage of apache camel as microservice framework providing integration with consul, hystrix, ribbon and other tools
Stars: ✭ 24 (-53.85%)
Mutual labels:  circuit-breaker
Recloser
A concurrent circuit breaker implemented with ring buffers
Stars: ✭ 51 (-1.92%)
Mutual labels:  circuit-breaker
Happy
the alchemist's happy path with elixir
Stars: ✭ 37 (-28.85%)
Mutual labels:  monad
Breaker
Circuit breaker for HTTP requests in Elixir
Stars: ✭ 30 (-42.31%)
Mutual labels:  circuit-breaker

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',
};
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].