2018-11-14 09:18:03 +00:00
|
|
|
package circuitbreaker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2022-04-05 10:30:08 +00:00
|
|
|
"time"
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/tracing"
|
2022-12-05 10:30:05 +00:00
|
|
|
"github.com/vulcand/oxy/v2/cbreaker"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
2022-04-05 10:30:08 +00:00
|
|
|
const typeName = "CircuitBreaker"
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
type circuitBreaker struct {
|
|
|
|
circuitBreaker *cbreaker.CircuitBreaker
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new circuit breaker middleware.
|
2019-07-10 07:26:04 +00:00
|
|
|
func New(ctx context.Context, next http.Handler, confCircuitBreaker dynamic.CircuitBreaker, name string) (http.Handler, error) {
|
2018-11-14 09:18:03 +00:00
|
|
|
expression := confCircuitBreaker.Expression
|
|
|
|
|
2019-09-13 17:28:04 +00:00
|
|
|
logger := log.FromContext(middlewares.GetLoggerCtx(ctx, name, typeName))
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Debug("Creating middleware")
|
2022-04-05 10:30:08 +00:00
|
|
|
logger.Debugf("Setting up with expression: %s", expression)
|
|
|
|
|
2022-12-05 10:30:05 +00:00
|
|
|
cbOpts := []cbreaker.Option{
|
2022-04-05 10:30:08 +00:00
|
|
|
cbreaker.Fallback(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
tracing.SetErrorWithEvent(req, "blocked by circuit-breaker (%q)", expression)
|
|
|
|
rw.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
|
|
|
|
if _, err := rw.Write([]byte(http.StatusText(http.StatusServiceUnavailable))); err != nil {
|
|
|
|
log.FromContext(req.Context()).Error(err)
|
|
|
|
}
|
|
|
|
})),
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2022-04-05 10:30:08 +00:00
|
|
|
if confCircuitBreaker.CheckPeriod > 0 {
|
|
|
|
cbOpts = append(cbOpts, cbreaker.CheckPeriod(time.Duration(confCircuitBreaker.CheckPeriod)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if confCircuitBreaker.FallbackDuration > 0 {
|
|
|
|
cbOpts = append(cbOpts, cbreaker.FallbackDuration(time.Duration(confCircuitBreaker.FallbackDuration)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if confCircuitBreaker.RecoveryDuration > 0 {
|
|
|
|
cbOpts = append(cbOpts, cbreaker.RecoveryDuration(time.Duration(confCircuitBreaker.RecoveryDuration)))
|
|
|
|
}
|
|
|
|
|
|
|
|
oxyCircuitBreaker, err := cbreaker.New(next, expression, cbOpts...)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &circuitBreaker{
|
|
|
|
circuitBreaker: oxyCircuitBreaker,
|
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *circuitBreaker) GetTracingInformation() (string, ext.SpanKindEnum) {
|
|
|
|
return c.name, tracing.SpanKindNoneEnum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *circuitBreaker) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
c.circuitBreaker.ServeHTTP(rw, req)
|
|
|
|
}
|