Skip to content

FabrWill/nestjs-circuit-breaker

Repository files navigation

Logo

Nesjs Circuit Breaker

Description

NestJs Circuit Breaker implementation

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

Parameters Description
maxErrorsBeforeOpen Max errors until circuit breaker open and only use the fallback function
errorExpirationTimeInMilliseconds Expiration time that will be used to decrement the count erros, this exists to avoid singular erros to be counted
maxAttemptsInHalfOpenState Max 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
circuitCheckTimeoutInMilliseconds Timeout until change the cricuit breaker status to HALF_OPEN.
fallbackFunction fallback 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
key unique key used to handle circuit breaker in cache

About

A Simply implementation of circuit breaker for resilience in nestjs applications

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published