httpserver

package
v0.0.0-...-c92af4d Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2021 License: AGPL-3.0, Apache-2.0, CC-BY-SA-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderRequestID = "X-Request-Id"
)

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 Errors

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

func HandlerWithContext

func HandlerWithContext(ctx context.Context, next http.Handler) http.Handler

HandlerWithContext returns an http.Handler that changes the request context to ctx (replacing http.Server's default context.Background()), then calls next.

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

Types

type ErrorResponse

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

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 RequestCounter

type RequestCounter interface {
	http.Handler

	// Current() returns the number of requests in progress.
	Current() int

	// Max() returns the maximum number of concurrent requests
	// that will be accepted.
	Max() int
}

RequestCounter is an http.Handler that tracks the number of requests in progress.

func NewRequestLimiter

func NewRequestLimiter(maxRequests int, handler http.Handler, reg *prometheus.Registry) RequestCounter

NewRequestLimiter returns a RequestCounter that delegates up to maxRequests at a time to the given handler, and responds 503 to all incoming requests beyond that limit.

"concurrent_requests" and "max_concurrent_requests" metrics are registered with the given reg, if reg is not nil.

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