1.0.0 • Published 8 months ago

nestjs-circuit-breaker v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Description

This project is a simple implementation of Circuit Breaker Pattern to be used as decorator in NestJS services

Installation

tested in node 16.4

$ npm install nestjs-circuit-breaker

Add to the App

export default class MyService {
    @CircuitBreaker({
        fallbackFunction: () => 'fallback return',
    })
    async execute() {
        ...
    }
}

Cache Manager

this application uses the NestJS Cache Manager by default. So your application need to configure this

Parameters in Circuit Breaker

ParametersDescription
maxErrorsBeforeOpenMax errors until circuit breaker open and only use the fallback function
errorExpirationTimeInMillisecondsExpiration time that will be used to decrement the count erros, this exists to avoid singular erros to be counted
maxAttemptsInHalfOpenStateMax number of parallel tryings in half open state. this doesnt change the flow of circuit breaker, so if have a error in any of the attempts the circuit breaker will be opened again
circuitCheckTimeoutInMillisecondsTimeout until change the cricuit breaker status to HALF_OPEN.
fallbackFunctionfallback function used when has an error in the service or when the circuit breaker is opened
logger(Optional) circuit breaker log, by default it uses the NestJS Logger
keyunique key used to handle circuit breaker in cache