traefik/old/middlewares/cbreaker.go

41 lines
1.2 KiB
Go
Raw Normal View History

2015-09-19 11:02:59 +00:00
package middlewares
2015-09-19 11:02:59 +00:00
import (
"net/http"
2018-11-14 09:18:03 +00:00
"github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/middlewares/tracing"
"github.com/vulcand/oxy/cbreaker"
2015-09-19 11:02:59 +00:00
)
// CircuitBreaker holds the oxy circuit breaker.
2015-09-19 11:02:59 +00:00
type CircuitBreaker struct {
circuitBreaker *cbreaker.CircuitBreaker
}
// NewCircuitBreaker returns a new CircuitBreaker.
func NewCircuitBreaker(next http.Handler, expression string, options ...cbreaker.CircuitBreakerOption) (*CircuitBreaker, error) {
circuitBreaker, err := cbreaker.New(next, expression, options...)
if err != nil {
return nil, err
}
return &CircuitBreaker{circuitBreaker}, nil
2015-09-19 11:02:59 +00:00
}
2018-01-10 16:48:04 +00:00
// NewCircuitBreakerOptions returns a new CircuitBreakerOption
func NewCircuitBreakerOptions(expression string) cbreaker.CircuitBreakerOption {
2018-06-11 09:36:03 +00:00
return cbreaker.Fallback(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tracing.LogEventf(r, "blocked by circuit-breaker (%q)", expression)
w.WriteHeader(http.StatusServiceUnavailable)
2018-08-06 18:00:03 +00:00
if _, err := w.Write([]byte(http.StatusText(http.StatusServiceUnavailable))); err != nil {
log.Error(err)
}
2018-06-11 09:36:03 +00:00
}))
2018-01-10 16:48:04 +00:00
}
2018-06-11 09:36:03 +00:00
func (cb *CircuitBreaker) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
2015-09-19 11:02:59 +00:00
cb.circuitBreaker.ServeHTTP(rw, r)
}