httpserver

package
v0.0.0-...-288f078 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: AGPL-3.0, Apache-2.0, CC-BY-SA-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderRequestID = "X-Request-Id"
)
View Source
const MinPriority = math.MinInt64

Variables

This section is empty.

Functions

func AddRequestIDs

func AddRequestIDs(h http.Handler) http.Handler

AddRequestIDs wraps an http.Handler, adding an X-Request-Id header to each request that doesn't already have one.

func Error

func Error(w http.ResponseWriter, error string, code int)

func ErrorWithStatus

func ErrorWithStatus(err error, status int) error

func Errorf

func Errorf(status int, tmpl string, args ...interface{}) error

func Errors

func Errors(w http.ResponseWriter, errors []string, code int)

func HandlerWithDeadline

func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler

HandlerWithDeadline cancels the request context if the request takes longer than the specified timeout without having its connection hijacked.

If timeout is 0, there is no deadline: HandlerWithDeadline is a no-op.

func Inspect

func Inspect(registry *prometheus.Registry, authToken string, next http.Handler) http.Handler

Inspect serves a report of current requests at "GET /_inspect/requests", and passes other requests through to the next handler.

If registry is not nil, Inspect registers metrics about current requests.

func Log

func Log(args ...interface{})

Log calls log.Println but first transforms strings so they are safer to write in logs (e.g., 'foo"bar' becomes '"foo\"bar"'). Arguments that aren't strings and don't have a (String() string) method are left alone.

func LogRequests

func LogRequests(h http.Handler) http.Handler

LogRequests wraps an http.Handler, logging each request and response.

func Logger

func Logger(req *http.Request) logrus.FieldLogger

func SetResponseLogFields

func SetResponseLogFields(ctx context.Context, fields logrus.Fields)

Types

type ErrorResponse

type ErrorResponse struct {
	Errors []string `json:"errors"`
}

type HTTPStatusError

type HTTPStatusError interface {
	error
	HTTPStatus() int
}

type Handler

type Handler interface {
	http.Handler

	// Returns an http.Handler that serves the Handler's metrics
	// data at /metrics and /metrics.json, and passes other
	// requests through to next.
	ServeAPI(token string, next http.Handler) http.Handler
}

func Instrument

func Instrument(registry *prometheus.Registry, logger *logrus.Logger, next http.Handler) Handler

Instrument returns a new Handler that passes requests through to the next handler in the stack, and tracks metrics of those requests.

For the metrics to be accurate, the caller must ensure every request passed to the Handler also passes through LogRequests(...), and vice versa.

If registry is nil, a new registry is created.

If logger is nil, logrus.StandardLogger() is used.

type IDGenerator

type IDGenerator struct {
	// Prefix is prepended to each returned ID.
	Prefix string
	// contains filtered or unexported fields
}

IDGenerator generates alphanumeric strings suitable for use as unique IDs (a given IDGenerator will never return the same ID twice).

func (*IDGenerator) Next

func (g *IDGenerator) Next() string

Next returns a new ID string. It is safe to call Next from multiple goroutines.

type RequestLimiter

type RequestLimiter struct {
	Handler http.Handler

	// Queue determines which queue a request is assigned to.
	Queue func(req *http.Request) *RequestQueue

	// Priority determines queue ordering. Requests with higher
	// priority are handled first. Requests with equal priority
	// are handled FIFO. If Priority is nil, all requests are
	// handled FIFO.
	Priority func(req *http.Request, queued time.Time) int64

	// "concurrent_requests", "max_concurrent_requests",
	// "queued_requests", and "max_queued_requests" metrics are
	// registered with Registry, if it is not nil.
	Registry *prometheus.Registry
	// contains filtered or unexported fields
}

RequestLimiter wraps http.Handler, limiting the number of concurrent requests being handled by the wrapped Handler. Requests that arrive when the handler is already at the specified concurrency limit are queued and handled in the order indicated by the Priority function.

Caller must not modify any RequestLimiter fields after calling its methods.

func (*RequestLimiter) ServeHTTP

func (rl *RequestLimiter) ServeHTTP(resp http.ResponseWriter, req *http.Request)

type RequestQueue

type RequestQueue struct {
	// Label for metrics. No two queues should have the same label.
	Label string

	// Maximum number of requests being handled at once. Beyond
	// this limit, requests will be queued.
	MaxConcurrent int

	// Maximum number of requests in the queue. Beyond this limit,
	// the lowest priority requests will return 503.
	MaxQueue int

	// Return 503 for any request for which Priority() returns
	// MinPriority if it spends longer than this in the queue
	// before starting processing.
	MaxQueueTimeForMinPriority time.Duration
	// contains filtered or unexported fields
}

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	WroteStatus() int
	WroteBodyBytes() int
	Sniffed() []byte
}

func WrapResponseWriter

func WrapResponseWriter(orig http.ResponseWriter) ResponseWriter

type Server

type Server struct {
	http.Server
	Addr string // host:port where the server is listening.
	// contains filtered or unexported fields
}

func (*Server) Close

func (srv *Server) Close() error

Close shuts down the server and returns when it has stopped.

func (*Server) Start

func (srv *Server) Start() error

Start is essentially (*http.Server)ListenAndServe() with two more features: (1) by the time Start() returns, Addr is changed to the address:port we ended up listening to -- which makes listening on ":0" useful in test suites -- and (2) the server can be shut down without killing the process -- which is useful in test cases, and makes it possible to shut down gracefully on SIGTERM without killing active connections.

func (*Server) Wait

func (srv *Server) Wait() error

Wait returns when the server has shut down.

Jump to

Keyboard shortcuts

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