Circuit breaker middleware
This commit is contained in:
parent
9d46c5017c
commit
616c58ad4b
5 changed files with 97 additions and 40 deletions
21
middlewares/cbreaker.go
Normal file
21
middlewares/cbreaker.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
Copyright
|
||||
*/
|
||||
package middlewares
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/mailgun/oxy/cbreaker"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue