# nestjs-circuit-breaker

> A implementation of circuit breaker pattern for simply purporses of resilience

Latest version **1.0.1** (published 2023-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-circuit-breaker
pnpm add nestjs-circuit-breaker
yarn add nestjs-circuit-breaker
bun add nestjs-circuit-breaker
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2023-09-28 |
| First published | 2023-09-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 223.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | William Fabre |
| Maintainers | williamfabre |
| Keywords | nestjs, circuit-breaker, circuit, breaker, javascript, typescript |

## Links

- npm: https://www.npmjs.com/package/nestjs-circuit-breaker
- Repository: https://github.com/FabrWill/nestjs-circuit-breaker
- Homepage: https://github.com/FabrWill/nestjs-circuit-breaker#readme
- Issues: https://github.com/FabrWill/nestjs-circuit-breaker/issues
- npm.io page: https://npm.io/package/nestjs-circuit-breaker

## Dependencies (3)

- [cache-manager](https://npm.io/package/cache-manager.md) ^5.2.3
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^10.2.6
- [@nestjs/cache-manager](https://npm.io/package/@nestjs/cache-manager.md) ^2.1.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2023-09-28
- 1.0.0 — 2023-09-28

## README

<p align="center">
  <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Logo" /></a>
</p>

[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
[circleci-url]: https://circleci.com/gh/nestjs/nest

  <p align="center">Nesjs Circuit Breaker </p>
    <p align="center">
</p>

## Description

<img src="dpr_circuit_breaker.png" width="720" alt="NestJs Circuit Breaker implementation" />

This project is a simple implementation of [Circuit Breaker Pattern](https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern) to be used as decorator in NestJS services
## Installation
tested in node 16.4

```bash
$ npm install nestjs-circuit-breaker
```

## Add to the App

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

## Cache Manager
this application uses the [NestJS Cache Manager](https://docs.nestjs.com/techniques/logger) 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](https://docs.nestjs.com/techniques/logger)                     |
| `key`                                   | unique key used to handle circuit breaker in cache|

---
_Source: https://npm.io/package/nestjs-circuit-breaker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
