Skip to content

Commit

Permalink
Add Circuit Breaker docs (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 authored Sep 12, 2023
1 parent b8bdd2a commit 8a6ed54
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions message/router/middleware/circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import (
"github.com/sony/gobreaker"
)

// CircuitBreaker is a middleware that wraps the handler in a circuit breaker.
// Based on the configuration, the circuit breaker will fail fast if the handler keeps returning errors.
// This is useful for preventing cascading failures.
type CircuitBreaker struct {
cb *gobreaker.CircuitBreaker
}

// NewCircuitBreaker returns a new CircuitBreaker middleware.
// Refer to the gobreaker documentation for the available settings.
func NewCircuitBreaker(settings gobreaker.Settings) CircuitBreaker {
return CircuitBreaker{
cb: gobreaker.NewCircuitBreaker(settings),
}
}

// Middleware returns the CircuitBreaker middleware.
func (c CircuitBreaker) Middleware(h message.HandlerFunc) message.HandlerFunc {
return func(msg *message.Message) ([]*message.Message, error) {
out, err := c.cb.Execute(func() (interface{}, error) {
Expand Down

0 comments on commit 8a6ed54

Please sign in to comment.