{"id":13414106,"url":"https://github.com/rubyist/circuitbreaker","last_synced_at":"2025-05-14T16:15:25.481Z","repository":{"id":18748183,"uuid":"21960219","full_name":"rubyist/circuitbreaker","owner":"rubyist","description":"Circuit Breakers in Go","archived":false,"fork":false,"pushed_at":"2024-05-15T19:00:14.000Z","size":111,"stargazers_count":1136,"open_issues_count":22,"forks_count":110,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-05T19:08:08.779Z","etag":null,"topics":["circuit-breakers","circuitbreaker","go"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rubyist.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-07-17T22:41:33.000Z","updated_at":"2025-03-31T13:07:51.000Z","dependencies_parsed_at":"2024-05-16T06:46:46.652Z","dependency_job_id":"6ed66c99-b216-488d-bde6-2c2d6e86a64f","html_url":"https://github.com/rubyist/circuitbreaker","commit_stats":{"total_commits":97,"total_committers":14,"mean_commits":6.928571428571429,"dds":"0.20618556701030932","last_synced_commit":"4afb8473475bafc430776dcaafd1e6d84a8032ad"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyist%2Fcircuitbreaker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyist%2Fcircuitbreaker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyist%2Fcircuitbreaker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyist%2Fcircuitbreaker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rubyist","download_url":"https://codeload.github.com/rubyist/circuitbreaker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631670,"owners_count":21136554,"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":["circuit-breakers","circuitbreaker","go"],"created_at":"2024-07-30T20:01:57.852Z","updated_at":"2025-04-12T20:40:24.694Z","avatar_url":"https://github.com/rubyist.png","language":"Go","funding_links":[],"categories":["Go","Utilities","Microservice","Utility","公用事业公司","实用工具","實用工具","工具库","工具库`可以提升效率的通用代码库和工具`","Repositories"],"sub_categories":["Utility/Miscellaneous","HTTP Clients","实用程序/Miscellaneous","Fail injection","高级控制台界面","Advanced Console UIs","高級控制台界面","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","查询语","交流"],"readme":"# circuitbreaker\n\nCircuitbreaker provides an easy way to use the Circuit Breaker pattern in a\nGo program.\n\nCircuit breakers are typically used when your program makes remote calls.\nRemote calls can often hang for a while before they time out. If your\napplication makes a lot of these requests, many resources can be tied\nup waiting for these time outs to occur. A circuit breaker wraps these\nremote calls and will trip after a defined amount of failures or time outs\noccur. When a circuit breaker is tripped any future calls will avoid making\nthe remote call and return an error to the caller. In the meantime, the\ncircuit breaker will periodically allow some calls to be tried again and\nwill close the circuit if those are successful.\n\nYou can read more about this pattern and how it's used at:\n- [Martin Fowler's bliki](http://martinfowler.com/bliki/CircuitBreaker.html)\n- [The Netflix Tech Blog](http://techblog.netflix.com/2012/02/fault-tolerance-in-high-volume.html)\n- [Release It!](http://pragprog.com/book/mnee/release-it)\n\n[![GoDoc](https://godoc.org/github.com/rubyist/circuitbreaker?status.svg)](https://godoc.org/github.com/rubyist/circuitbreaker)\n\n## Installation\n\n```\n  go get github.com/rubyist/circuitbreaker\n```\n\n## Examples\n\nHere is a quick example of what circuitbreaker provides\n\n```go\n// Creates a circuit breaker that will trip if the function fails 10 times\ncb := circuit.NewThresholdBreaker(10)\n\nevents := cb.Subscribe()\ngo func() {\n  for {\n    e := \u003c-events\n    // Monitor breaker events like BreakerTripped, BreakerReset, BreakerFail, BreakerReady\n  }\n}()\n\ncb.Call(func() error {\n\t// This is where you'll do some remote call\n\t// If it fails, return an error\n}, 0)\n```\n\nCircuitbreaker can also wrap a time out around the remote call.\n\n```go\n// Creates a circuit breaker that will trip after 10 failures\n// using a time out of 5 seconds\ncb := circuit.NewThresholdBreaker(10)\n\ncb.Call(func() error {\n  // This is where you'll do some remote call\n  // If it fails, return an error\n}, time.Second * 5) // This will time out after 5 seconds, which counts as a failure\n\n// Proceed as above\n\n```\n\nCircuitbreaker can also trip based on the number of consecutive failures.\n\n```go\n// Creates a circuit breaker that will trip if 10 consecutive failures occur\ncb := circuit.NewConsecutiveBreaker(10)\n\n// Proceed as above\n```\n\nCircuitbreaker can trip based on the error rate.\n\n```go\n// Creates a circuit breaker based on the error rate\ncb := circuit.NewRateBreaker(0.95, 100) // trip when error rate hits 95%, with at least 100 samples\n\n// Proceed as above\n```\n\nIf it doesn't make sense to wrap logic in Call(), breakers can be handled manually.\n\n```go\ncb := circuit.NewThresholdBreaker(10)\n\nfor {\n  if cb.Ready() {\n    // Breaker is not tripped, proceed\n    err := doSomething()\n    if err != nil {\n      cb.Fail() // This will trip the breaker once it's failed 10 times\n      continue\n    }\n    cb.Success()\n  } else {\n    // Breaker is in a tripped state.\n  }\n}\n```\n\nCircuitbreaker also provides a wrapper around `http.Client` that will wrap a\ntime out around any request.\n\n```go\n// Passing in nil will create a regular http.Client.\n// You can also build your own http.Client and pass it in\nclient := circuit.NewHTTPClient(time.Second * 5, 10, nil)\n\nresp, err := client.Get(\"http://example.com/resource.json\")\n```\n\nSee the godoc for more examples.\n\n## Bugs, Issues, Feedback\n\nRight here on GitHub: [https://github.com/rubyist/circuitbreaker](https://github.com/rubyist/circuitbreaker)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubyist%2Fcircuitbreaker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frubyist%2Fcircuitbreaker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubyist%2Fcircuitbreaker/lists"}