{"id":13601638,"url":"https://github.com/valor-software/nest-circuit-breaker","last_synced_at":"2025-06-23T17:11:43.667Z","repository":{"id":57309999,"uuid":"191361417","full_name":"valor-software/nest-circuit-breaker","owner":"valor-software","description":"nest-circuit-breaker","archived":false,"fork":false,"pushed_at":"2020-07-17T06:58:02.000Z","size":18,"stargazers_count":25,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"development","last_synced_at":"2024-03-26T01:02:16.442Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/valor-software.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-11T11:51:33.000Z","updated_at":"2024-02-19T15:11:06.000Z","dependencies_parsed_at":"2022-09-09T21:31:45.754Z","dependency_job_id":null,"html_url":"https://github.com/valor-software/nest-circuit-breaker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valor-software%2Fnest-circuit-breaker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valor-software%2Fnest-circuit-breaker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valor-software%2Fnest-circuit-breaker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valor-software%2Fnest-circuit-breaker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/valor-software","download_url":"https://codeload.github.com/valor-software/nest-circuit-breaker/tar.gz/refs/heads/development","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223458486,"owners_count":17148474,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T18:01:05.543Z","updated_at":"2024-11-07T04:31:19.749Z","avatar_url":"https://github.com/valor-software.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# nest-circuitbreaker\n\nThis is a wrapper for implementaion of [Circuit Breaker][wiki] pattern(made by [hystrixjs][hystrix]) adapted\nfor NestJS framework\n\n## Implementation assumptions and constraints\n\n* Every protected method must return a Promise as a result (so async functions and methods are fine \nand this is very convenient with NestJS)\n* We can protect only methods of instances that are within NestJS components (annotated with @Component).\nThis constraint comes from NestJS itself, cause it gives enough control (and possibilities for augmentation)\nonly over @Components.\n* Points above imply that if some of the functionality inside of a @Controller instance needs to be protected\nwith a Circuit Breaker - this functionality must be extracted in to service (@Component instance).\n\n## Protecting services (@Component instances) with CircuitBreaker\n\n1. Augment service that needs protection.\n1. Annotate methods that need protection inside of the service with @CuircuitBreakerProtected annotation\n1. Provide an optional configuration for the @CuircuitBreakerProtected annotation\n1. Start watching over circuit breaker events (they will be logged)\n\n## Augment service that needs protection\n#### Regular component creation\n```\n@Module({\n modules: [CoreModule],\n controllers: [\n   CandidatesController\n ],\n components: [\n   CandidatesService\n ]\n})\nexport class NewCandidatesModule {\n}\n```\n#### Circuit Breaker protected component creation\n\n```\n@Module({\n modules: [CoreModule],\n controllers: [\n   CandidatesController\n ],\n components: [\n   {\n     provide: CandidatesService,\n     useFactory: (logger: Logger, config: Config, api: ApiHelperService) =\u003e {\n       return addCircuitBreakerSupportTo(new CandidatesService(config, api), CandidatesService);\n     },\n     inject: [\n       Logger, Config, ApiHelperService\n     ]\n   }\n ]\n})\nexport class NewCandidatesModule {\n}\n```\n\n## Annotate methods that need protection\n#### Annotated service method\n```\n@Component()\nexport class CandidatesService {\n // ... \n\n @CircuitBreakerProtected({fallbackTo: defaultFallback})\n async searchResumesNode(body: any) {\n   return await request({\n\t// required request options\n   });\n }\n\n // ...\n}\n```\n\n## @CircuitBreakerProtected annotation configuration\n```\n// All settings are optional and their default values provided below\n@CircuitBreakerProtected({\n circuitBreakerSleepWindowInMilliseconds: 3000,\n circuitBreakerErrorThresholdPercentage: 50,\n circuitBreakerRequestVolumeThreshold: 10,\n timeout: 10000,\n statisticalWindowLength: 10000,\n statisticalWindowNumberOfBuckets: 10,\n percentileWindowLength: 10000,\n percentileWindowNumberOfBuckets: 10,\n requestVolumeRejectionThreshold: 0,\n fallbackTo: undefined,\n shouldErrorBeConsidered: undefined\n})\n```\n\n## Start watching over circuit breaker events\n``` \nexport class CoreModule {\n constructor(private logger: Logger,\n             private config: Config,\n             private sendgridService: SendgridService) {\n   const startObservingCircuitBreakerState = makeCircuitBreakerStateObserver(this.sendgridService, logger, config);\n   \n   // Every 5 seconds (be default) Circuit Breaker will be polled and all opened ones (this is when back off is active)\n   //get logged along with health stats\n   startObservingCircuitBreakerState();\n }\n}\n```\n\n## When Circuit Breaker is active (back off is active)\n#### This is what gets printed to the console:\n```\n\u003e \"CandidatesService.searchResumesNode\" circuit breaker is active.\n\u003e Health stats: {totalCount: 100, errorCount: 80, errorsPercentage: 80}\n```\n\n[wiki]: https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern\n[hystrix]: https://www.npmjs.com/package/hystrixjs\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalor-software%2Fnest-circuit-breaker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvalor-software%2Fnest-circuit-breaker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalor-software%2Fnest-circuit-breaker/lists"}