busy

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NopRequestDone

func NopRequestDone()

NopRequestDone is a RequestDone implementation that does nothing. Useful to use instead of nil.

Types

type Limiter

type Limiter interface {
	// Check enforces a concurrent request limit, possibly using information
	// from the given HTTP request.  If this method returns true, it indicates
	// that the request is allowed.  In that case, the RequestDone must be non-nil
	// and must be invoked by calling code to update the Limiter's state.
	//
	// If this method returns false, the request should not proceed.  The
	// RequestDone should be ignored.
	//
	// The RequestDone returned by this method is not guaranteed to be idempotent.
	// Callers must take care to invoke it exactly once for any given request.
	Check(*http.Request) (RequestDone, bool)
}

Limiter is a request limiter. It constrains the number of concurrent requests either globally or based on some aspect of each request, such as a user account.

type MaxRequestLimiter

type MaxRequestLimiter struct {
	// MaxRequests is the maximum number of concurrent HTTP requests.  If this
	// is nonpositive, then all requests are allowed.
	MaxRequests int64
	// contains filtered or unexported fields
}

MaxRequestLimiter is a Limiter that imposes a global limit for maximum concurrent requests. No aspect of each HTTP request is taken into account.

func (*MaxRequestLimiter) Check

func (rcl *MaxRequestLimiter) Check(*http.Request) (RequestDone, bool)

Check verifies that no more than MaxRequests requests are currently inflight. If MaxRequests is nonpositive, this method returns NopRequestDone and true.

type RequestDone

type RequestDone func()

RequestDone is a callback that must be invoked when a request is finished so that its corresponding Limiter can update its state. Care must be taken by clients to invoke a RequestDone exactly once for any request.

type Server

type Server struct {
	// Limiter is the concurrent request limiting strategy.  If this field is unset,
	// then no limiting is done.
	Limiter Limiter

	// Busy is the optional http.Handler to invoke when the maximum number
	// concurrent requests has been exceeded.  ConstantHandler is a useful choice
	// for this field, as it allows one to tailor not only the status code but also
	// the headers and body.
	//
	// If this field is nil, this middleware simply returns http.StatusServiceUnavailable.
	Busy http.Handler
}

Server defines a server middleware that enforces request limiting

func (Server) Then

func (s Server) Then(next http.Handler) http.Handler

Then is a server middleware that enforces this busy configuration. If Limiter is nil, no decoration is done and next is returned as is. If OnBusy is nil, then the returned handler will simply set http.StatusServiceUnavailable when requests fail the limit check.

Jump to

Keyboard shortcuts

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