middleware

package
v0.0.0-...-44b4573 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MPL-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PrometheusHandlerTypeLabel is the name of data label given to prometheus observers, the label data shall be the symbol name of the HTTP handler's type.
	PrometheusHandlerTypeLabel = "handler_type"
	// PrometheusHandlerLocationLabel is the name of data label given to prometheus observers, the label data shall be the URL location at which HTTP handler is installed.
	PrometheusHandlerLocationLabel = "url_location"
	// MaxLatestRequests is the maximum number of latest HTTP requests to be
	// kept in-memory for on-demand inspection.
	MaxLatestRequests = 200
)

Variables

View Source
var (
	// LatestRequests is a ring buffer of the latest HTTP requests processed by
	// HTTP daemons. HandleLatestRequestsInspector will be retrieving these
	// captured requests and present them to user for inspection.
	LatestRequests *datastruct.RingBuffer

	// EnableLatestRequestsRecording determines whether the default set of
	// middleware installed to HTTP daemons will be capturing the latest HTTP
	// requests into a ring buffer for inspection. HandleLatestRequestsInspector
	// will be turning it on and off.
	EnableLatestRequestsRecording bool
)

Functions

func EmergencyLockdown

func EmergencyLockdown(next http.HandlerFunc) http.HandlerFunc

EmergencyLockdown decorates the HTTP handler function by determining whether the program-wide emergency lock-down is in-effect. If the lock-down is in effect, the HTTP client will get an empty (albeit successful) response, without invoking the next handler function.

func GetRealClientIP

func GetRealClientIP(r *http.Request) string

GetRealClientIP returns the IP of HTTP client that initiated the HTTP request. Usually, the return value is identical to IP portion of RemoteAddr, but if there is a proxy server in between, such as a load balancer or LAN proxy, the return value will be the client IP address read from header "X-Real-Ip" (preferred) or "X-Forwarded-For".

func LogRequestStats

func LogRequestStats(logger *lalog.Logger, next http.HandlerFunc) http.HandlerFunc

LogRequestStats decorates the HTTP handler function by logging several the request parameters - enough to identify the handler and request origin, as well as execution stats such as time-to-first-byte.

func RateLimit

func RateLimit(rateLimit *lalog.RateLimit, next http.HandlerFunc) http.HandlerFunc

RateLimit decorates the HTTP handler function by applying a rate limit to the client, identified by its IP. If the client has made too many requests, it will respond to the client with HTTP status too-many-requests, without invoking the next handler function.

func RecordInternalStats

func RecordInternalStats(stats *misc.Stats, next http.HandlerFunc) http.HandlerFunc

RecordInternalStats decorates the HTTP handler function by recording the request handing duration in internal stats.

func RecordLatestRequests

func RecordLatestRequests(logger *lalog.Logger, next http.HandlerFunc) http.HandlerFunc

RecordLatestRequests records the request body for on-demand inspection.

func RecordPrometheusStats

func RecordPrometheusStats(
	handlerTypeLabel, handlerLocationLabel string,
	durationHistogram, timeToFirstByteHistogram, responseSizeHistogram *prometheus.HistogramVec,
	next http.HandlerFunc) http.HandlerFunc

RecordPrometheusStats decorates the HTTP handler function by recording its execution stats with prometheus. The recorded stats will be exposed on an HTTP endpoint dedicated to reading prometheus metrics.

func RestrictMaxRequestSize

func RestrictMaxRequestSize(maxRequestBodyBytes int, next http.HandlerFunc) http.HandlerFunc

RestrictMaxRequestSize decorates the HTTP handler function by restricting how much of the request body can be read by the next handler function. This helps to prevent a malfunctioning HTTP client coupled with a faulty handler to use excessive amount of system memory.

func WithAWSXray

func WithAWSXray(next http.HandlerFunc) http.HandlerFunc

WithAWSXray decorates the HTTP handler function using AWS x-ray library for distributed tracing.

Types

type ConnRecorder

type ConnRecorder struct {
	net.Conn
	// contains filtered or unexported fields
}

ConnRecorder is a net.Conn that remembers the size and timing characteristics of bytes written.

func (*ConnRecorder) Write

func (rec *ConnRecorder) Write(b []byte) (n int, err error)

type HTTPInterceptRecorder

type HTTPInterceptRecorder struct {
	http.Hijacker
	ConnRecorder *ConnRecorder
}

HTTPInterceptRecorder is an http.Hijacker that comes with the capability of inspecting the size and timing characteristics of the intercepted stream.

func (*HTTPInterceptRecorder) Hijack

func (rec *HTTPInterceptRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack allows the caller to take over the HTTP connection.

type HTTPResponseRecorder

type HTTPResponseRecorder struct {
	http.Hijacker
	http.ResponseWriter
	// contains filtered or unexported fields
}

HTTPResponseRecorder is an http.ResponseWriter that comes with the capability of inspecting the size and timing characteristics of the HTTP response.

func (*HTTPResponseRecorder) Write

func (rec *HTTPResponseRecorder) Write(b []byte) (int, error)

Write memorises the time-to-1st-byte and accumulated size of the response, and then invokes the underlying ResponseWriter using the same data buffer.

func (*HTTPResponseRecorder) WriteHeader

func (rec *HTTPResponseRecorder) WriteHeader(statusCode int)

WriteHeader memorises the status code in the recorder and then invokes the underlying ResponseWriter using the same status code.

Jump to

Keyboard shortcuts

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