{"id":17548665,"url":"https://github.com/djo/circuit","last_synced_at":"2026-07-01T02:31:08.881Z","repository":{"id":143181087,"uuid":"125430961","full_name":"djo/circuit","owner":"djo","description":"Implementation of the Circuit Breaker in Go based on sync/atomic","archived":false,"fork":false,"pushed_at":"2018-03-16T21:43:00.000Z","size":5,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-10-09T16:14:47.619Z","etag":null,"topics":[],"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/djo.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":"2018-03-15T22:00:48.000Z","updated_at":"2019-06-02T17:14:05.000Z","dependencies_parsed_at":"2023-05-31T13:01:29.296Z","dependency_job_id":null,"html_url":"https://github.com/djo/circuit","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/djo/circuit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djo%2Fcircuit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djo%2Fcircuit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djo%2Fcircuit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djo%2Fcircuit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/djo","download_url":"https://codeload.github.com/djo/circuit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djo%2Fcircuit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34990845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-01T02:00:05.325Z","response_time":130,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-10-21T02:24:41.906Z","updated_at":"2026-07-01T02:31:08.649Z","avatar_url":"https://github.com/djo.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"circuit\n=======\n\n[![GoDoc](https://godoc.org/github.com/djo/circuit?status.svg)](http://godoc.org/github.com/djo/circuit)\n[![Build Status](https://travis-ci.org/djo/circuit.svg?branch=master)](https://travis-ci.org/djo/circuit)\n\nImplementation of [the circuit breaker](https://docs.microsoft.com/en-us/azure/architecture/patterns/circuit-breaker) pattern in Go.\n\nThe circuit breaker can prevent an application from repeatedly trying to execute an operation that's likely to fail.\nIt is implemented as a state machine with the following states:\n\n- `Closed`: the request from the application is allowed to pass.\n- `Half-Open`: a limited number of requests are allowed to pass.\n- `Open`: the request is failed immediately and an error returned.\n\nUsage\n-----\n\n```go\nfunc NewBreaker(interval time.Duration, cooldown time.Duration, atLeastReqs uint32, toOpen ToState, toClosed ToState) (*Breaker, error)\n```\n\n- `interval` is the cyclic period of the closed state.\n- `cooldown` is the period of the open state,\n- `atLeastReqs` is the number of requests to consider in the half-open state\n  before invoking a given toClosed function for decision making.\n- `toOpen` is called whenever a request fails in the closed state.\n  If it returns true, the circuit breaker will be placed into the open state.\n- `toClosed` is called in the half-open state once the number of requests reached atLeastReqs.\n  If it returns true, the circuit breaker will be placed into the closed state,\n  otherwise into the open state.\n\nA function signature of `toOpen` and `toClosed`:\n\n```go\nfunc(total uint32, failures uint32) bool\n```\n\n`Execute` runs a given request if the circuit breaker accepts it,\ncases when it's in the closed state, or half-open one\nand the number of requests has not yet reached `atLeastReqs`.\n\nReturns ErrBreakerOpen when it doesn't accept the request,\notherwise the error from the req function:\n\n```go\nfunc (b *Breaker) Execute(req func() error) error\n```\n\nExample\n-------\n\n```go\n// open the circuit breaker in case of 5% of failed requests\ntoOpen := func(total uint32, failures uint32) bool {\n\treturn total \u003e 0 \u0026\u0026 float64(failures)/float64(total) \u003e= 0.05\n}\n\n// close the circuit breaker only if no failures\ntoClosed := func(total uint32, failures uint32) bool {\n\treturn failures == 0\n}\n\nb, err := circuit.NewBreaker(time.Minute, 10*time.Second, 1, toOpen, toClosed)\nif err != nil {\n\tpanic(err)\n}\n\nfunc GetStatus(url string) (string, error) {\n\tvar resp *http.Response\n\terr = b.Execute(func() error {\n\t\tresp, err = http.Get(url)\n\t\treturn err\n\t})\n\n\tif err == circuit.ErrBreakerOpen {\n\t\t// the circuit breaker failed fast,\n\t\t// there is still time for fallback\n\t\treturn \"200 (cache)\", nil\n\t}\n\n\tif err != nil {\n\t\t// the request failed\n\t\treturn \"\", err\n\t}\n\n\treturn resp.Status, nil\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjo%2Fcircuit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdjo%2Fcircuit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjo%2Fcircuit/lists"}