2018-11-14 09:18:03 +00:00
|
|
|
package buffering
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
2019-09-13 17:28:04 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/log"
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/middlewares"
|
|
|
|
"github.com/containous/traefik/v2/pkg/tracing"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
|
|
|
oxybuffer "github.com/vulcand/oxy/buffer"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
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")
|
2019-09-05 11:42:04 +00:00
|
|
|
logger.Debugf("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),
|
|
|
|
oxybuffer.CondSetter(len(config.RetryExpression) > 0, oxybuffer.Retry(config.RetryExpression)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &buffer{
|
|
|
|
name: name,
|
|
|
|
buffer: oxyBuffer,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) GetTracingInformation() (string, ext.SpanKindEnum) {
|
|
|
|
return b.name, tracing.SpanKindNoneEnum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
b.buffer.ServeHTTP(rw, req)
|
|
|
|
}
|