traefik/pkg/middlewares/retry/retry.go

258 lines
7.1 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package retry
import (
"bufio"
"context"
"fmt"
2021-03-04 19:08:03 +00:00
"io"
"math"
2018-11-14 09:18:03 +00:00
"net"
"net/http"
"net/http/httptrace"
"time"
2018-11-14 09:18:03 +00:00
"github.com/cenkalti/backoff/v4"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/config/dynamic"
"github.com/traefik/traefik/v3/pkg/middlewares"
"github.com/traefik/traefik/v3/pkg/tracing"
2024-01-08 08:10:06 +00:00
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
2018-11-14 09:18:03 +00:00
)
// Compile time validation that the response writer implements http interfaces correctly.
var _ middlewares.Stateful = &responseWriter{}
2018-11-14 09:18:03 +00:00
const typeName = "Retry"
2018-11-14 09:18:03 +00:00
// Listener is used to inform about retry attempts.
type Listener interface {
// Retried will be called when a retry happens, with the request attempt passed to it.
// For the first retry this will be attempt 2.
Retried(req *http.Request, attempt int)
}
// Listeners is a convenience type to construct a list of Listener and notify
// each of them about a retry attempt.
type Listeners []Listener
// retry is a middleware that retries requests.
type retry struct {
attempts int
initialInterval time.Duration
next http.Handler
listener Listener
name string
2018-11-14 09:18:03 +00:00
}
// New returns a new retry middleware.
func New(ctx context.Context, next http.Handler, config dynamic.Retry, listener Listener, name string) (http.Handler, error) {
2022-11-21 17:36:05 +00:00
middlewares.GetLogger(ctx, name, typeName).Debug().Msg("Creating middleware")
2018-11-14 09:18:03 +00:00
if config.Attempts <= 0 {
return nil, fmt.Errorf("incorrect (or empty) value for attempt (%d)", config.Attempts)
}
return &retry{
attempts: config.Attempts,
initialInterval: time.Duration(config.InitialInterval),
next: next,
listener: listener,
name: name,
2018-11-14 09:18:03 +00:00
}, nil
}
func (r *retry) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
2021-11-10 14:34:10 +00:00
if r.attempts == 1 {
r.next.ServeHTTP(rw, req)
return
}
closableBody := req.Body
defer closableBody.Close()
2021-03-04 19:08:03 +00:00
// if we might make multiple attempts, swap the body for an io.NopCloser
// cf https://github.com/traefik/traefik/issues/1008
2021-11-10 14:34:10 +00:00
req.Body = io.NopCloser(closableBody)
2018-11-14 09:18:03 +00:00
attempts := 1
2024-01-08 08:10:06 +00:00
initialCtx := req.Context()
tracer := tracing.TracerFromContext(initialCtx)
var currentSpan trace.Span
2021-11-10 14:34:10 +00:00
operation := func() error {
2024-01-08 08:10:06 +00:00
if tracer != nil {
if currentSpan != nil {
currentSpan.End()
}
// Because multiple tracing spans may need to be created,
// the Retry middleware does not implement trace.Traceable,
// and creates directly a new span for each retry operation.
var tracingCtx context.Context
tracingCtx, currentSpan = tracer.Start(initialCtx, typeName, trace.WithSpanKind(trace.SpanKindInternal))
currentSpan.SetAttributes(attribute.String("traefik.middleware.name", r.name))
// Only add the attribute "http.resend_count" defined by semantic conventions starting from second attempt.
if attempts > 1 {
currentSpan.SetAttributes(semconv.HTTPResendCount(attempts - 1))
}
req = req.WithContext(tracingCtx)
}
2021-11-10 14:34:10 +00:00
shouldRetry := attempts < r.attempts
retryResponseWriter := newResponseWriter(rw, shouldRetry)
// Disable retries when the backend already received request data
2024-01-08 08:10:06 +00:00
clientTrace := &httptrace.ClientTrace{
2021-11-10 14:34:10 +00:00
WroteHeaders: func() {
retryResponseWriter.DisableRetries()
},
WroteRequest: func(httptrace.WroteRequestInfo) {
retryResponseWriter.DisableRetries()
},
}
2024-01-08 08:10:06 +00:00
newCtx := httptrace.WithClientTrace(req.Context(), clientTrace)
2018-11-14 09:18:03 +00:00
r.next.ServeHTTP(retryResponseWriter, req.Clone(newCtx))
2021-11-10 14:34:10 +00:00
if !retryResponseWriter.ShouldRetry() {
return nil
}
2021-11-10 14:34:10 +00:00
attempts++
2021-11-10 14:34:10 +00:00
return fmt.Errorf("attempt %d failed", attempts-1)
}
2022-11-21 17:36:05 +00:00
logger := middlewares.GetLogger(req.Context(), r.name, typeName)
2021-11-10 14:34:10 +00:00
backOff := backoff.WithContext(r.newBackOff(), req.Context())
2018-11-14 09:18:03 +00:00
2021-11-10 14:34:10 +00:00
notify := func(err error, d time.Duration) {
2022-11-21 17:36:05 +00:00
logger.Debug().Msgf("New attempt %d for request: %v", attempts, req.URL)
2019-09-13 17:28:04 +00:00
2021-11-10 14:34:10 +00:00
r.listener.Retried(req, attempts)
}
2019-09-13 17:28:04 +00:00
2022-08-29 09:36:08 +00:00
err := backoff.RetryNotify(operation, backOff, notify)
2021-11-10 14:34:10 +00:00
if err != nil {
2022-11-21 17:36:05 +00:00
logger.Debug().Err(err).Msg("Final retry attempt failed")
}
2024-01-08 08:10:06 +00:00
if currentSpan != nil {
currentSpan.End()
}
}
2021-11-10 14:34:10 +00:00
func (r *retry) newBackOff() backoff.BackOff {
if r.attempts < 2 || r.initialInterval <= 0 {
return &backoff.ZeroBackOff{}
2018-11-14 09:18:03 +00:00
}
b := backoff.NewExponentialBackOff()
b.InitialInterval = r.initialInterval
// calculate the multiplier for the given number of attempts
// so that applying the multiplier for the given number of attempts will not exceed 2 times the initial interval
// it allows to control the progression along the attempts
b.Multiplier = math.Pow(2, 1/float64(r.attempts-1))
// according to docs, b.Reset() must be called before using
b.Reset()
return b
2018-11-14 09:18:03 +00:00
}
// Retried exists to implement the Listener interface. It calls Retried on each of its slice entries.
func (l Listeners) Retried(req *http.Request, attempt int) {
for _, listener := range l {
listener.Retried(req, attempt)
}
}
func newResponseWriter(rw http.ResponseWriter, shouldRetry bool) *responseWriter {
return &responseWriter{
2018-11-14 09:18:03 +00:00
responseWriter: rw,
headers: make(http.Header),
2018-11-14 09:18:03 +00:00
shouldRetry: shouldRetry,
}
}
type responseWriter struct {
2018-11-14 09:18:03 +00:00
responseWriter http.ResponseWriter
headers http.Header
2018-11-14 09:18:03 +00:00
shouldRetry bool
2019-02-01 08:50:04 +00:00
written bool
2018-11-14 09:18:03 +00:00
}
func (r *responseWriter) ShouldRetry() bool {
2018-11-14 09:18:03 +00:00
return r.shouldRetry
}
func (r *responseWriter) DisableRetries() {
2018-11-14 09:18:03 +00:00
r.shouldRetry = false
}
func (r *responseWriter) Header() http.Header {
2019-02-01 08:50:04 +00:00
if r.written {
return r.responseWriter.Header()
}
return r.headers
2018-11-14 09:18:03 +00:00
}
func (r *responseWriter) Write(buf []byte) (int, error) {
2018-11-14 09:18:03 +00:00
if r.ShouldRetry() {
return len(buf), nil
}
return r.responseWriter.Write(buf)
}
func (r *responseWriter) WriteHeader(code int) {
2018-11-14 09:18:03 +00:00
if r.ShouldRetry() && code == http.StatusServiceUnavailable {
// We get a 503 HTTP Status Code when there is no backend server in the pool
// to which the request could be sent. Also, note that r.ShouldRetry()
// will never return true in case there was a connection established to
// the backend server and so we can be sure that the 503 was produced
// inside Traefik already and we don't have to retry in this cases.
r.DisableRetries()
}
if r.ShouldRetry() || r.written {
2018-11-14 09:18:03 +00:00
return
}
// In that case retry case is set to false which means we at least managed
// to write headers to the backend : we are not going to perform any further retry.
// So it is now safe to alter current response headers with headers collected during
// the latest try before writing headers to client.
headers := r.responseWriter.Header()
for header, value := range r.headers {
headers[header] = value
}
2018-11-14 09:18:03 +00:00
r.responseWriter.WriteHeader(code)
// Handling informational headers.
// This allows to keep writing to r.headers map until a final status code is written.
if code >= 100 && code <= 199 {
return
}
2019-02-01 08:50:04 +00:00
r.written = true
2018-11-14 09:18:03 +00:00
}
func (r *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
2018-11-14 09:18:03 +00:00
hijacker, ok := r.responseWriter.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("%T is not a http.Hijacker", r.responseWriter)
}
return hijacker.Hijack()
}
func (r *responseWriter) Flush() {
2018-11-14 09:18:03 +00:00
if flusher, ok := r.responseWriter.(http.Flusher); ok {
flusher.Flush()
}
}