{"id":13469972,"url":"https://github.com/sony/gobreaker","last_synced_at":"2025-05-12T05:32:38.935Z","repository":{"id":32888464,"uuid":"36482920","full_name":"sony/gobreaker","owner":"sony","description":"Circuit Breaker implemented in Go","archived":false,"fork":false,"pushed_at":"2025-03-25T01:55:38.000Z","size":54,"stargazers_count":3131,"open_issues_count":28,"forks_count":192,"subscribers_count":35,"default_branch":"master","last_synced_at":"2025-05-12T02:45:22.592Z","etag":null,"topics":["circuit-breaker","golang","microservice"],"latest_commit_sha":null,"homepage":"","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/sony.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2015-05-29T04:46:15.000Z","updated_at":"2025-05-11T16:18:26.000Z","dependencies_parsed_at":"2024-05-03T09:01:54.206Z","dependency_job_id":"8ee270f0-be95-4482-be4f-976d372642be","html_url":"https://github.com/sony/gobreaker","commit_stats":{"total_commits":49,"total_committers":14,"mean_commits":3.5,"dds":"0.26530612244897955","last_synced_commit":"3cecc1db8db2b65d844215123a89c9682f8cbb58"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sony%2Fgobreaker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sony%2Fgobreaker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sony%2Fgobreaker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sony%2Fgobreaker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sony","download_url":"https://codeload.github.com/sony/gobreaker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253672731,"owners_count":21945482,"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-breaker","golang","microservice"],"created_at":"2024-07-31T16:00:20.223Z","updated_at":"2025-05-12T05:32:38.907Z","avatar_url":"https://github.com/sony.png","language":"Go","funding_links":[],"categories":["开源类库","Go","Open source library","golang"],"sub_categories":["限流器","Current Limiter"],"readme":"gobreaker\n=========\n\n[![GoDoc](https://godoc.org/github.com/sony/gobreaker/v2?status.svg)](https://godoc.org/github.com/sony/gobreaker/v2)\n\n[gobreaker][repo-url] implements the [Circuit Breaker pattern](https://msdn.microsoft.com/en-us/library/dn589784.aspx) in Go.\n\nInstallation\n------------\n\n```\ngo get github.com/sony/gobreaker/v2\n```\n\nUsage\n-----\n\nThe struct `CircuitBreaker` is a state machine to prevent sending requests that are likely to fail.\nThe function `NewCircuitBreaker` creates a new `CircuitBreaker`.\nThe type parameter `T` specifies the return type of requests.\n\n```go\nfunc NewCircuitBreaker[T any](st Settings) *CircuitBreaker[T]\n```\n\nYou can configure `CircuitBreaker` by the struct `Settings`:\n\n```go\ntype Settings struct {\n\tName          string\n\tMaxRequests   uint32\n\tInterval      time.Duration\n\tTimeout       time.Duration\n\tReadyToTrip   func(counts Counts) bool\n\tOnStateChange func(name string, from State, to State)\n\tIsSuccessful  func(err error) bool\n}\n```\n\n- `Name` is the name of the `CircuitBreaker`.\n\n- `MaxRequests` is the maximum number of requests allowed to pass through\n  when the `CircuitBreaker` is half-open.\n  If `MaxRequests` is 0, `CircuitBreaker` allows only 1 request.\n\n- `Interval` is the cyclic period of the closed state\n  for `CircuitBreaker` to clear the internal `Counts`, described later in this section.\n  If `Interval` is 0, `CircuitBreaker` doesn't clear the internal `Counts` during the closed state.\n\n- `Timeout` is the period of the open state,\n  after which the state of `CircuitBreaker` becomes half-open.\n  If `Timeout` is 0, the timeout value of `CircuitBreaker` is set to 60 seconds.\n\n- `ReadyToTrip` is called with a copy of `Counts` whenever a request fails in the closed state.\n  If `ReadyToTrip` returns true, `CircuitBreaker` will be placed into the open state.\n  If `ReadyToTrip` is `nil`, default `ReadyToTrip` is used.\n  Default `ReadyToTrip` returns true when the number of consecutive failures is more than 5.\n\n- `OnStateChange` is called whenever the state of `CircuitBreaker` changes.\n\n- `IsSuccessful` is called with the error returned from a request.\n  If `IsSuccessful` returns true, the error is counted as a success.\n  Otherwise the error is counted as a failure.\n  If `IsSuccessful` is nil, default `IsSuccessful` is used, which returns false for all non-nil errors.\n\nThe struct `Counts` holds the numbers of requests and their successes/failures:\n\n```go\ntype Counts struct {\n\tRequests             uint32\n\tTotalSuccesses       uint32\n\tTotalFailures        uint32\n\tConsecutiveSuccesses uint32\n\tConsecutiveFailures  uint32\n}\n```\n\n`CircuitBreaker` clears the internal `Counts` either\non the change of the state or at the closed-state intervals.\n`Counts` ignores the results of the requests sent before clearing.\n\n`CircuitBreaker` can wrap any function to send a request:\n\n```go\nfunc (cb *CircuitBreaker[T]) Execute(req func() (T, error)) (T, error)\n```\n\nThe method `Execute` runs the given request if `CircuitBreaker` accepts it.\n`Execute` returns an error instantly if `CircuitBreaker` rejects the request.\nOtherwise, `Execute` returns the result of the request.\nIf a panic occurs in the request, `CircuitBreaker` handles it as an error\nand causes the same panic again.\n\nExample\n-------\n\n```go\nvar cb *gobreaker.CircuitBreaker[[]byte]\n\nfunc Get(url string) ([]byte, error) {\n\tbody, err := cb.Execute(func() ([]byte, error) {\n\t\tresp, err := http.Get(url)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tdefer resp.Body.Close()\n\t\tbody, err := io.ReadAll(resp.Body)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\treturn body, nil\n\t})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn body, nil\n}\n```\n\nSee [example](https://github.com/sony/gobreaker/blob/master/v2/example) for details.\n\nLicense\n-------\n\nThe MIT License (MIT)\n\nSee [LICENSE](https://github.com/sony/gobreaker/blob/master/LICENSE) for details.\n\n\n[repo-url]: https://github.com/sony/gobreaker\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsony%2Fgobreaker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsony%2Fgobreaker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsony%2Fgobreaker/lists"}