v2.0.0-alpha.2 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2021 License: Apache-2.0 Imports: 10 Imported by: 0



Package buffer provides http.Handler middleware that solves several problems when dealing with http requests:

Reads the entire request and response into buffer, optionally buffering it to disk for large requests. Checks the limits for the requests and responses, rejecting in case if the limit was exceeded. Changes request content-transfer-encoding from chunked and provides total size to the handlers.

Examples of a buffering middleware:

// sample HTTP handler
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

// Buffer will read the body in buffer before passing the request to the handler
// calculate total size of the request and transform it from chunked encoding
// before passing to the server

// This version will buffer up to 2MB in memory and will serialize any extra
// to a temporary file, if the request size exceeds 10MB it will reject the request
	buffer.MemRequestBodyBytes(2 * 1024 * 1024),
	buffer.MaxRequestBodyBytes(10 * 1024 * 1024),

// Will do the same as above, but with responses
	buffer.MemResponseBodyBytes(2 * 1024 * 1024),
	buffer.MaxResponseBodyBytes(10 * 1024 * 1024),

// Buffer will replay the request if the handler returns error at least 3 times
// before returning the response
buffer.New(handler, buffer.Retry(`IsNetworkError() && Attempts() <= 2`))



View Source
const (
	// DefaultMemBodyBytes Store up to 1MB in RAM
	DefaultMemBodyBytes = 1048576
	// DefaultMaxBodyBytes No limit by default
	DefaultMaxBodyBytes = -1
	// DefaultMaxRetryAttempts Maximum retry attempts
	DefaultMaxRetryAttempts = 10


This section is empty.


func CondSetter

func CondSetter(condition bool, setter optSetter) optSetter

CondSetter Conditional setter. ex: Cond(a > 4, MemRequestBodyBytes(a))

func ErrorHandler

func ErrorHandler(h utils.ErrorHandler) optSetter

ErrorHandler sets error handler of the server

func IsValidExpression

func IsValidExpression(expr string) bool

IsValidExpression check if it's a valid expression

func Logger

func Logger(l utils.Logger) optSetter

Logger defines the logger the buffer will use.

func MaxRequestBodyBytes

func MaxRequestBodyBytes(m int64) optSetter

MaxRequestBodyBytes sets the maximum request body size in bytes

func MaxResponseBodyBytes

func MaxResponseBodyBytes(m int64) optSetter

MaxResponseBodyBytes sets the maximum response body size in bytes

func MemRequestBodyBytes

func MemRequestBodyBytes(m int64) optSetter

MemRequestBodyBytes bytes sets the maximum request body to be stored in memory buffer middleware will serialize the excess to disk.

func MemResponseBodyBytes

func MemResponseBodyBytes(m int64) optSetter

MemResponseBodyBytes sets the maximum response body to be stored in memory buffer middleware will serialize the excess to disk.

func Retry

func Retry(predicate string) optSetter

Retry provides a predicate that allows buffer middleware to replay the request if it matches certain condition, e.g. returns special error code. Available functions are:

Attempts() - limits the amount of retry attempts ResponseCode() - returns http response code IsNetworkError() - tests if response code is related to networking error

Example of the predicate:

`Attempts() <= 2 && ResponseCode() == 502`


type Buffer

type Buffer struct {
	// contains filtered or unexported fields

Buffer is responsible for buffering requests and responses It buffers large requests and responses to disk,

func New

func New(next http.Handler, setters ...optSetter) (*Buffer, error)

New returns a new buffer middleware. New() function supports optional functional arguments

func (*Buffer) ServeHTTP

func (b *Buffer) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Buffer) Wrap

func (b *Buffer) Wrap(next http.Handler) error

Wrap sets the next handler to be called by buffer handler.

type SizeErrHandler

type SizeErrHandler struct{}

SizeErrHandler Size error handler

func (*SizeErrHandler) ServeHTTP

func (e *SizeErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL