Documentation ¶
Overview ¶
package stream 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 streaming middleware:
// sample HTTP handler handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("hello")) }) // Stream 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 stream.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 stream.New(handler, stream.MemRequestBodyBytes(2 * 1024 * 1024), stream.MaxRequestBodyBytes(10 * 1024 * 1024)) // Will do the same as above, but with responses stream.New(handler, stream.MemResponseBodyBytes(2 * 1024 * 1024), stream.MaxResponseBodyBytes(10 * 1024 * 1024)) // Stream will replay the request if the handler returns error at least 3 times // before returning the response stream.New(handler, stream.Retry(`IsNetworkError() && Attempts() <= 2`))
Index ¶
- Constants
- func ErrorHandler(h utils.ErrorHandler) optSetter
- func IsValidExpression(expr string) bool
- func Logger(l utils.Logger) optSetter
- func MaxRequestBodyBytes(m int64) optSetter
- func MaxResponseBodyBytes(m int64) optSetter
- func MemRequestBodyBytes(m int64) optSetter
- func MemResponseBodyBytes(m int64) optSetter
- func Retry(predicate string) optSetter
- type SizeErrHandler
- type Streamer
Constants ¶
const ( // Store up to 1MB in RAM DefaultMemBodyBytes = 1048576 // No limit by default DefaultMaxBodyBytes = -1 // Maximum retry attempts DefaultMaxRetryAttempts = 10 )
Variables ¶
This section is empty.
Functions ¶
func ErrorHandler ¶
func ErrorHandler(h utils.ErrorHandler) optSetter
ErrorHandler sets error handler of the server
func IsValidExpression ¶
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 request body size in bytes
func MemRequestBodyBytes ¶
func MemRequestBodyBytes(m int64) optSetter
MaxRequestBody bytes sets the maximum request body to be stored in memory stream middleware will serialize the excess to disk.
func MemResponseBodyBytes ¶
func MemResponseBodyBytes(m int64) optSetter
MemResponseBodyBytes sets the maximum request body to be stored in memory stream middleware will serialize the excess to disk.
func Retry ¶
func Retry(predicate string) optSetter
Retry provides a predicate that allows stream 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`
Types ¶
type SizeErrHandler ¶
type SizeErrHandler struct { }
func (*SizeErrHandler) ServeHTTP ¶
func (e *SizeErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error)