22 lines
536 B
Go
22 lines
536 B
Go
/*
|
|
Copyright
|
|
*/
|
|
package middlewares
|
|
|
|
import (
|
|
"github.com/mailgun/oxy/cbreaker"
|
|
"net/http"
|
|
)
|
|
|
|
type CircuitBreaker struct {
|
|
circuitBreaker *cbreaker.CircuitBreaker
|
|
}
|
|
|
|
func NewCircuitBreaker(next http.Handler, options ...cbreaker.CircuitBreakerOption) *CircuitBreaker {
|
|
circuitBreaker, _ := cbreaker.New(next, "NetworkErrorRatio() > 0.5", options...)
|
|
return &CircuitBreaker{circuitBreaker}
|
|
}
|
|
|
|
func (cb *CircuitBreaker) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
cb.circuitBreaker.ServeHTTP(rw, r)
|
|
}
|