buffer

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: Apache-2.0 Imports: 9 Imported by: 4

Documentation

Overview

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) {
  w.Write([]byte("hello"))
})

// 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
buffer.New(handler)

// 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.New(handler,
  buffer.MemRequestBodyBytes(2 * 1024 * 1024),
  buffer.MaxRequestBodyBytes(10 * 1024 * 1024))

// Will do the same as above, but with responses
buffer.New(handler,
  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`))

Index

Constants

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
)

Variables

This section is empty.

Functions

func IsValidExpression

func IsValidExpression(expr string) bool

IsValidExpression check if it's a valid expression.

Types

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 ...Option) (*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 Option

type Option func(b *Buffer) error

Option represents an option you can pass to New.

func Cond

func Cond(condition bool, setter Option) Option

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

func ErrorHandler

func ErrorHandler(h utils.ErrorHandler) Option

ErrorHandler sets error handler of the server.

func Logger

func Logger(l utils.Logger) Option

Logger defines the logger used by Buffer.

func MaxRequestBodyBytes

func MaxRequestBodyBytes(m int64) Option

MaxRequestBodyBytes sets the maximum request body size in bytes.

func MaxResponseBodyBytes

func MaxResponseBodyBytes(m int64) Option

MaxResponseBodyBytes sets the maximum response body size in bytes.

func MemRequestBodyBytes

func MemRequestBodyBytes(m int64) Option

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) Option

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) Option

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`.

func Verbose

func Verbose(verbose bool) Option

Verbose additional debug information.

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