Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shirokovnv/circuit_breaker

Circuit-breaker pattern, written in Go
https://github.com/shirokovnv/circuit_breaker

circuit-breaker go pattern

Last synced: 1 day ago
JSON representation

Circuit-breaker pattern, written in Go

Awesome Lists containing this project

README

        

# Circuit Breaker

Implements [circuit-breaker pattern][link-pattern] with [Golang][link-go]

Inpsired by [sony/gobreaker][link-inspired]

## Dependencies

- [Go][link-go] >= 1.19
- [Make][link-make]

## Architecture

![Diagram](circuit_breaker.png)

## Example

```go
func main() {
config := circuit_breaker.Config{
Name: "Circuit Breaker",
ReadyToTrip: func(counts circuit_breaker.Counts) bool {
failureRatio := float64(counts.TotalFailures) / float64(counts.Requests)
return counts.Requests >= 3 && failureRatio >= 0.7
},
OnStateChange: func(name string, from, to circuit_breaker.State) {
fmt.Printf("%s: state changed from %s to %s", name, from, to)
},
}

cb := circuit_breaker.NewCircuitBreaker(config)

msg, err := cb.Execute(func() (interface{}, error) {
// your actual service call here...
})
}
```

See [example][link-example] for details.

## License

MIT. Please see the [license file](license.md) for more information.

[link-go]: https://go.dev/
[link-make]: https://www.gnu.org/software/make/manual/make.html
[link-inspired]: https://github.com/sony/gobreaker/
[link-example]: /example/main.go
[link-pattern]: https://microservices.io/patterns/reliability/circuit-breaker.html