buffering

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT Imports: 8 Imported by: 1

README

Documentation

Overview

Package buffering provides http.Handler that buffers bodies of http requests to on-disk files before passing this request to child handler.

This may be useful if request is proxied to some backend that should read request as fast as possible and should not be bound by slow link of the client (i.e. Python backend).

Index

Constants

View Source
const DefaultBufSize = 32 * 1024 // 32K

DefaultBufSize sets default max request body size which is buffered using in-memory buffers for newly created Handler. Use WithBufSize option to adjust Handler configuration.

Variables

This section is empty.

Functions

func Handler

func Handler(h http.Handler, opts ...Option) http.Handler

Handler returns http.Handler wrapping original one; this handler first reads request body storing it to in-memory buffer or temporary file, then calls original handler to process request body directly from buffer or file.

If no options are given, Handler defaults to creating files in default temporary directory, has no max payload or concurrency limit and uses in-memory buffers for payloads not exceeding DefaultBufSize.

Types

type Option

type Option func(*handler)

Option modifies handler logic

func WithBufSize

func WithBufSize(size int) Option

WithBufSize configures Handler to user in-memory buffers for payloads which do not exceed given size. Only bodies with Content-Length header are handled using in-memory buffers, chunked payloads fall back to files.

func WithDir

func WithDir(dir string) Option

WithDir configures Handler to use given directory to store temporary files. It doesn't check whether directory is accessible.

func WithFileLimit

func WithFileLimit(maxFiles int, wait time.Duration) Option

WithFileLimit limits max number of files Handler is allowed to keep opened. This may be useful to limit number of file descriptors and OS threads, since file IO in Go consume real threads. Optional wait duration specifies how long request would be waiting in the queue for available slot until giving up. If 0, wait time is not limited (until request is canceled).

func WithMaxSize

func WithMaxSize(size int64) Option

WithMaxSize configures Handler to limit max allowed request body size. If request body exceeds given size, Handler replies with 413 Payload Too Large.

func WithRequiredContentLength

func WithRequiredContentLength() Option

WithRequiredContentLength configures Handler to require Content-Length to be explicitly set for POST and PUT requests. POST and PUT requests without Content-Length header will get 411 Length Required response (RFC 7231, 6.5.10). This allows Handler to always use in-memory buffers when their size is sufficient.

Jump to

Keyboard shortcuts

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