{"id":22351875,"url":"https://github.com/screwdriver-cd/circuit-fuses","last_synced_at":"2025-07-30T07:31:49.526Z","repository":{"id":44609522,"uuid":"63731428","full_name":"screwdriver-cd/circuit-fuses","owner":"screwdriver-cd","description":"Wrapper around node-circuitbreaker to define a simple callback interface","archived":false,"fork":false,"pushed_at":"2022-12-09T17:45:21.000Z","size":61,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":18,"default_branch":"master","last_synced_at":"2024-11-14T10:12:53.393Z","etag":null,"topics":["javascript","nodejs"],"latest_commit_sha":null,"homepage":"https://cd.screwdriver.cd/pipelines/22","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/screwdriver-cd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-19T22:12:25.000Z","updated_at":"2022-02-04T19:40:29.000Z","dependencies_parsed_at":"2023-01-25T22:45:29.186Z","dependency_job_id":null,"html_url":"https://github.com/screwdriver-cd/circuit-fuses","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/screwdriver-cd%2Fcircuit-fuses","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/screwdriver-cd%2Fcircuit-fuses/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/screwdriver-cd%2Fcircuit-fuses/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/screwdriver-cd%2Fcircuit-fuses/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/screwdriver-cd","download_url":"https://codeload.github.com/screwdriver-cd/circuit-fuses/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228106387,"owners_count":17870438,"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":["javascript","nodejs"],"created_at":"2024-12-04T12:16:11.722Z","updated_at":"2024-12-04T12:16:12.487Z","avatar_url":"https://github.com/screwdriver-cd.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Circuit-fuses\n[![Version][npm-image]][npm-url] ![Downloads][downloads-image] [![Build Status][status-image]][status-url] [![Open Issues][issues-image]][issues-url] ![License][license-image]\n\n\u003e Wrapper around [screwdriver-node-circuitbreaker] to define a callback interface\n\n## Usage\n\n```bash\nnpm install circuit-fuses\n```\n\nThis module wraps the [screwdriver-node-circuitbreaker] and provides a simple callback interface for handling the circuit breaker.\n\n```js\nconst Breaker = require('circuit-fuses').breaker;\nconst request = require('request');\nconst command = request.get\n// To setup the fuse, instantiate a new Breaker with the command to run\nconst breaker = new Breaker(command);\n\nbreaker.runCommand('http://yahoo.com', (err, resp) =\u003e {\n    if (err) {\n        /* If the circuit is open the command is not run, and an error\n         * with message \"CircuitBreaker Open\" is returned.\n         * In this case, you can switch on the error and have a fallback technique\n         */\n        // ... stuff\n    }\n    // Here there is no error and it's possible to proceed with the resp object\n});\n```\n\nThe `runCommand` method will return a promise if a callback is not supplied.\n\n```js\nconst Breaker = require('circuit-fuses').breaker;\nconst request = require('request');\nconst command = request.get\n// To setup the fuse, instantiate a new Breaker with the command to run\nconst breaker = new Breaker(command);\n\nbreaker.runCommand('http://yahoo.com')\n    .then(resp =\u003e {\n        // Here there is no error and it's possible to proceed with the resp object\n    })\n    .catch(err =\u003e {\n        /* If the circuit is open the command is not run, and an error\n         * with message \"CircuitBreaker Open\" is returned.\n         * In this case, you can switch on the error and have a fallback technique\n         */\n        // ... stuff\n    });\n```\n\n### Constructor\n`constructor(command, options)` \n\n| Parameter        | Type  | Required  |  Description | Default |\n| :-------------   | :---- | :---- | :-------------| :---------- |\n| command        | Function | Yes | The command to run with circuit breaker | none |\n| options.breaker | Object | No | The object to configure the breaker module with | {} |\n| options.breaker.timeout | Number | No | The timeout in ms to wait for a command | 10000 |\n| options.breaker.maxFailures | Number | No | The number of failures before the breaker switches |  5  |\n| options.breaker.resetTimeout | Number | No | The number in ms to wait before resetting the circuit |  50 |\n| options.retry | Object | No | The object to configure the retry module with | {} |\n| options.retry.retries | Number | No | The number of retries to do before passing back a failure | 5 |\n| options.retry.factor | Number | No | The exponential factor to use | 2 |\n| options.retry.minTimeout | Number | No | The timeout to wait before doing the first retry | 1000 |\n| options.retry.maxTimeout | Number | No | The max timeout to wait before retrying | Number.MAX_VALUE |\n| options.retry.randomize | Boolean | No | Randomize the timeout | false |\n| options.shouldRetry | Function | No | Special logic to should circuit retries | () =\u003e true |\n\n### Short circuiting the retry logic\nNormally, the circuit breaker will retry with exponential back off until the max number of retries has been reached or the breaker has opened due to max failures. You may have operations that you want to immediately fail under certain conditions. In this situation, a shouldRetry option is available. This is a function that receives the err object and arguments passed to the runCommand function.\n\nFor example, short circuit when runCommand is called with \"MyArg\" and the error contains a status code of 404:\n\n```js\nshouldRetry(err, args) {\n    return args[0] === \"MyArg\" \u0026\u0026 err.statusCode === 404;\n}\n```\n\n### Run Command\nTo run the command passed to the constructor\n\n`runCommand(...args, callback)`\n\n| Parameter        | Type  | Required  |  Description |\n| :-------------   | :---- | :---- | :-------------|\n| args        | Arguments | No | The arguments to pass to the command |\n| callback | Function | No | The callback to call when the command returns |\n\n### Get Total Number Requests\nReturns the total number of requests from the circuit breaker\n\n`getTotalRequests()`\n\n### Get Total Number Request Timeouts\nReturns the total number of request timeouts that occurred\n\n`getTimeouts()`\n\n### Get Total Number Request Successful\nReturns the total number of successful requests that occurred\n\n`getSuccessfulRequests()`\n\n### Get Total Number Request Failed\nReturns the total number of failed requests that occurred\n\n`getFailedRequests()`\n\n### Get Total Concurrent Requests\nReturns the total number of concurrent requests that occurred\n\n`getConcurrentRequests()`\n\n### Get Average Request time\nReturns the average request time taken\n\n`getAverageRequestTime()`\n\n### Get whether the breaker is closed\nReturns boolean whether the circuit breaker is closed\n\n`isClosed()`\n\n### Get a holistic set of the above metrics\n`stats()`\n\n## Using Fuseboxes\n\nA FuseBox is a collection of circuit breakers. If one circuit breaker in the fuse box breaks, the others break as well. To create a new fusebox:\n\n```\nconst FuseBox = require('circuit-fuses').box;\nconst fusebox = new FuseBox();\n```\n\nTo add circuit breakers to the fuse box, use the `addFuse()` method.\n\n`addFuse(circuitbreaker)`\n\n| Parameter        | Type  | Required  |  Description |\n| :-------------   | :---- | :---- | :-------------|\n| circuitbreaker   | CircuitBreaker | Yes | The circuit breaker to add to the fuse box |\n\nHere's an example:\n\n```\nvar breaker1 = new Breaker('testFn');\nvar breaker2 = new Breaker('testFn2');\nvar breaker3 = new Breaker('testFn3');\nfusebox.addFuse(breaker1);\nfusebox.addFuse(breaker2);\n```\nIn the above case, if breaker1 trips, breaker2 will trip as well because both of them belong to the same fuse box.\n\n## Testing\n\n```bash\nnpm test\n```\n\n## License\n\nCode licensed under the BSD 3-Clause license. See LICENSE file for terms.\n\n[npm-image]: https://img.shields.io/npm/v/circuit-fuses.svg\n[npm-url]: https://npmjs.org/package/circuit-fuses\n[downloads-image]: https://img.shields.io/npm/dt/circuit-fuses.svg\n[license-image]: https://img.shields.io/npm/l/circuit-fuses.svg\n[issues-image]: https://img.shields.io/github/issues/screwdriver-cd/screwdriver.svg\n[issues-url]: https://github.com/screwdriver-cd/screwdriver/issues\n[status-image]: https://cd.screwdriver.cd/pipelines/22/badge\n[status-url]: https://cd.screwdriver.cd/pipelines/22\n[screwdriver-node-circuitbreaker]: https://github.com/screwdriver-cd/node-circuitbreaker\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscrewdriver-cd%2Fcircuit-fuses","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscrewdriver-cd%2Fcircuit-fuses","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscrewdriver-cd%2Fcircuit-fuses/lists"}