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
- func IsValidExpression(expr string) bool
- type Buffer
- type Option
- func Cond(condition bool, setter Option) Option
- func ErrorHandler(h utils.ErrorHandler) Option
- func Logger(l utils.Logger) Option
- func MaxRequestBodyBytes(m int64) Option
- func MaxResponseBodyBytes(m int64) Option
- func MemRequestBodyBytes(m int64) Option
- func MemResponseBodyBytes(m int64) Option
- func Retry(predicate string) Option
- func Verbose(verbose bool) Option
- type SizeErrHandler
Constants ¶
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 ¶
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,.
type Option ¶
Option represents an option you can pass to New.
func ErrorHandler ¶
func ErrorHandler(h utils.ErrorHandler) Option
ErrorHandler sets error handler of the server.
func MaxRequestBodyBytes ¶
MaxRequestBodyBytes sets the maximum request body size in bytes.
func MaxResponseBodyBytes ¶
MaxResponseBodyBytes sets the maximum response body size in bytes.
func MemRequestBodyBytes ¶
MemRequestBodyBytes bytes sets the maximum request body to be stored in memory buffer middleware will serialize the excess to disk.
func MemResponseBodyBytes ¶
MemResponseBodyBytes sets the maximum response body to be stored in memory buffer middleware will serialize the excess to disk.
func Retry ¶
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 SizeErrHandler ¶
type SizeErrHandler struct{}
SizeErrHandler Size error handler.
func (*SizeErrHandler) ServeHTTP ¶
func (e *SizeErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error)