httpgovernor

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2020 License: LGPL-3.0 Imports: 7 Imported by: 0

README

HTTP Governor

The httpgovernor package provides an HTTP request concurreny limiter. This is used to protect resources served over HTTP from becoming overloaded.

Documentation

Overview

Package httpgovernor provides concurrency limiting for a http.Handler.

Index

Constants

This section is empty.

Variables

View Source
var DefaultOverloadHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
	w.WriteHeader(http.StatusServiceUnavailable)
	w.Write([]byte("Overloaded"))
})

DefaultOverloadHandler is the default handler used in an overload condition.

Functions

func New

func New(p Params, hnd http.Handler) http.Handler

New creates a new http.Handler that wraps the given handler limiting the amount of concurrent requests that will be handled.

Types

type CostEstimator

type CostEstimator interface {
	// EstimateCost calculates the relative cost of a request, that is
	// the amount of concurrency points required to acquire before
	// servicing the request. If the cost is 0 then the request will
	// be actioned, even if there are others queued.
	EstimateCost(req *http.Request) int64
}

A CostEstimator is used to determine the cost of a request.

type Counter

type Counter interface {
	// Inc increments the counter by 1.
	Inc()
}

A Counter is used to monitor a monotonically increasing value.

type Gauge

type Gauge interface {
	// Inc increments the gauge value by 1.
	Inc()
	// Dec decrements the gauge value by 1.
	Dec()
}

A Gauge is used to monitor the size of the request queue.

type Observer

type Observer interface {
	// Observe records the length of time (in seconds) a request had
	// to wait in the queue.
	Observe(float64)
}

An Observer is used to monitor the time taken for an action to complete.

type Params

type Params struct {
	// MaxConcurrency specifies the maximum level of concurrency
	// allowed by the governor. If this is 0 then concurrency will
	// not be governed.
	MaxConcurrency int64

	// MaxBurst specifies the maximum level of concurrency before
	// requests are failed without queueing. If this is 0 then no
	// requests will be queued. The maximum queue size is roughly
	// equivilent to MaxBurst-MaxConcurrency.
	MaxBurst int64

	// MaxQueueDuration specifies the maximum time a request should
	// be queued before being aborted. If this is 0 then a default
	// duration of 10s will be used.
	MaxQueueDuration time.Duration

	// OverloadHandler is the http.Handler used to handle requests
	// that have to be dropped due to the server being overloaded. If
	// this is nil then DefaultOverloadHandler will be used.
	OverloadHandler http.Handler

	// CostEstimator is used to determine the relative cost of a
	// request. If this is nil all requests will be assumed to have a
	// cost of 1.
	CostEstimator CostEstimator

	// RequestOverloadCounter is a counter that is incremented for
	// every request dropped because the server is overloaded.
	RequestOverloadCounter Counter

	// QueueLengthGauge is used to monitor the number of requests
	// queued by the governor.
	QueueLengthGauge Gauge

	// QueueDurationObserver is used to monitor the time succesful
	// requests are queued before being actioned.
	QueueDurationObserver Observer
}

type PathCostEstimator

type PathCostEstimator map[string]int64

A PathCostEstimator determines the cost of a request by matching the path of the URL.

func (PathCostEstimator) EstimateCost

func (c PathCostEstimator) EstimateCost(req *http.Request) int64

EstimateCost determines the cost of the given request by matching in the PathCostEstimator. Any path not specified is assumed to have a cost of 1.

type PatternCostEstimator added in v0.2.0

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

A PattenCostEstimator determines the cost of a request by matching the request against a list of configured patterns.

The supported patterns are similar to the ones used in http.ServeMux. A path must either be a rooted path, or a rooted subtree. As in http.ServeMux the longest match takes precedence.

A pattern may include a host before the path. If a host is specified only requests addressed to that host will be matched. Any host-specific match will take precedence over all-host matches.

func (*PatternCostEstimator) EstimateCost added in v0.2.0

func (c *PatternCostEstimator) EstimateCost(req *http.Request) int64

EstimateCost determines the cost of the given request by matching in the PatternCostEstimator. Any path not known is assumed to have a cost of 1.

func (*PatternCostEstimator) SetCost added in v0.2.0

func (c *PatternCostEstimator) SetCost(path string, cost int64)

SetCost configures the cost of a matched pattern.

Jump to

Keyboard shortcuts

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