middlewares

package
v0.0.0-...-7f3e199 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2019 License: LGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CacheRecentRatio is the fraction of recently added entries in the cache
	//
	// See more at https://godoc.org/github.com/hashicorp/golang-lru#pkg-constants
	CacheRecentRatio = 0.15

	// CacheGhostRatio is the fraction of ghost entries in the cache
	//
	// See more at https://godoc.org/github.com/hashicorp/golang-lru#pkg-constants
	CacheGhostRatio = 0.5
)

Variables

View Source
var (
	// ErrCacheItemExpired is returned when it was called an `Apply()` on an expired cache item.
	ErrCacheItemExpired = errors.New(`cache item has expired`)
)

Functions

func AccessLogger

func AccessLogger(logger Printfer, handler fasthttp.RequestHandler) fasthttp.RequestHandler

AccessLogger wraps the `handler` to log requests via `logger` (like `access.log`)

func Cache

func Cache(
	maxEntries uint64,
	maxEntrySize uint64,
	maxDuration time.Duration,
	handler fasthttp.RequestHandler,
) fasthttp.RequestHandler

Cache wraps the `handler` returns a new handler which will cache responses (and return responses from the cache if there's an actual record) for a time interval `maxDuration`.

To limit memory usage there're parameters `maxEntries` and `maxEntrySize`. Total consumed memory (by this middleware) could be estimated as `maxEntries * maxEntrySize`.

Also this middleware controls HTTP header `Cache-Control` to ask a browser to cache the response on it's side, too.

If there were received two similar (with the same URI) concurrent requests then it will wait for the execution of one of them then will just return the cached value to the second one.

func RecoverPanic

func RecoverPanic(handler fasthttp.RequestHandler) fasthttp.RequestHandler

RecoverPanic recovers panics in the `handler` and returns `500 Internal Server Error` if a panic occurs.

Types

type Printfer

type Printfer interface {
	// Printf is a pretty-standart `Printf` (`fmt` is the format and `args` are the arguments for the format).
	//
	// For example you can use `log.New` as `Printfer`.
	Printf(fmt string, args ...interface{})
}

Printfer is a simple logger interface for `NewHTTPServer`

Jump to

Keyboard shortcuts

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