stream

package
v0.0.0-...-8b1022e Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2016 License: Apache-2.0, Apache-2.0 Imports: 7 Imported by: 0

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

View Source
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 IsValidExpression(expr string) bool

func Logger

func Logger(l utils.Logger) optSetter

Logger sets the logger that will be used by this middleware.

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)

type Streamer

type Streamer struct {
	// contains filtered or unexported fields
}

Streamer is responsible for streaming requests and responses It buffers large reqeuests and responses to disk,

func New

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

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

func (*Streamer) ServeHTTP

func (s *Streamer) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Streamer) Wrap

func (s *Streamer) Wrap(next http.Handler) error

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

Jump to

Keyboard shortcuts

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