2018-11-14 09:18:03 +00:00
|
|
|
package buffering
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/logs"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares"
|
2022-11-21 17:36:05 +00:00
|
|
|
oxybuffer "github.com/vulcand/oxy/v2/buffer"
|
2024-01-08 08:10:06 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeName = "Buffer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type buffer struct {
|
|
|
|
name string
|
|
|
|
buffer *oxybuffer.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a buffering middleware.
|
2019-07-10 07:26:04 +00:00
|
|
|
func New(ctx context.Context, next http.Handler, config dynamic.Buffering, name string) (http.Handler, error) {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := middlewares.GetLogger(ctx, name, typeName)
|
|
|
|
logger.Debug().Msg("Creating middleware")
|
|
|
|
logger.Debug().Msgf("Setting up buffering: request limits: %d (mem), %d (max), response limits: %d (mem), %d (max) with retry: '%s'",
|
2018-11-14 09:18:03 +00:00
|
|
|
config.MemRequestBodyBytes, config.MaxRequestBodyBytes, config.MemResponseBodyBytes, config.MaxResponseBodyBytes, config.RetryExpression)
|
|
|
|
|
|
|
|
oxyBuffer, err := oxybuffer.New(
|
|
|
|
next,
|
|
|
|
oxybuffer.MemRequestBodyBytes(config.MemRequestBodyBytes),
|
|
|
|
oxybuffer.MaxRequestBodyBytes(config.MaxRequestBodyBytes),
|
|
|
|
oxybuffer.MemResponseBodyBytes(config.MemResponseBodyBytes),
|
|
|
|
oxybuffer.MaxResponseBodyBytes(config.MaxResponseBodyBytes),
|
2022-11-21 17:36:05 +00:00
|
|
|
oxybuffer.Logger(logs.NewOxyWrapper(*logger)),
|
|
|
|
oxybuffer.Verbose(logger.GetLevel() == zerolog.TraceLevel),
|
|
|
|
oxybuffer.Cond(len(config.RetryExpression) > 0, oxybuffer.Retry(config.RetryExpression)),
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &buffer{
|
|
|
|
name: name,
|
|
|
|
buffer: oxyBuffer,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
func (b *buffer) GetTracingInformation() (string, string, trace.SpanKind) {
|
|
|
|
return b.name, typeName, trace.SpanKindInternal
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
b.buffer.ServeHTTP(rw, req)
|
|
|
|
}
|