httpext

package
v0.0.0-...-fb5e452 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: Apache-2.0 Imports: 14 Imported by: 28

Documentation

Overview

Package httpext provides some convenience functions on top of the "net/http" package from the stdlib.

Index

Constants

This section is empty.

Variables

View Source
var ShutdownTimeout = 30 * time.Second

ShutdownTimeout is the timeout that ListenAndServeContext() will impose on server.Shutdown() before forcefully terminating request handlers that are still in progress.

The default timeout is quite lenient to accommodate long-running requests, but it can be lowered for servers running in an interactive terminal session where a quick response to Ctrl-C is more important.

Functions

func ContextWithSIGINT

func ContextWithSIGINT(ctx context.Context, delay time.Duration) context.Context

ContextWithSIGINT creates a new context.Context using the provided Context, and launches a goroutine that cancels the Context when an interrupt signal is caught.

This function is not strictly related to net/http, but fits nicely with func ListenAndServeContext from this package.

If `delay` is not 0, the context will be canceled with this delay after an interrupt signal was caught. This is useful when using the context with ListenAndServeContext(), to give reverse-proxies using this HTTP server some extra delay to notice the pending shutdown of this server.

func GetRequesterIPFor

func GetRequesterIPFor(r *http.Request) string

GetRequesterIPFor inspects an http.Request and returns the IP address of the machine where the request originated (or the empty string if no IP can be found in the request).

func LimitConcurrentRequestsMiddleware

func LimitConcurrentRequestsMiddleware(maxRequests int) func(http.Handler) http.Handler

LimitConcurrentRequestsMiddleware is a HTTP server middleware that limits the amount of concurrent requests. If the given number of requests is already running, and a new request comes in, it has to wait until one of the previous requests is finished. This is useful for small sidecar API processes that are confined to tight resource budgets.

func ListenAndServeContext

func ListenAndServeContext(ctx context.Context, addr string, handler http.Handler) error

ListenAndServeContext is a wrapper around http.ListenAndServe() that additionally shuts down the HTTP server gracefully when the context expires, or if an error occurs.

func ListenAndServeTLSContext

func ListenAndServeTLSContext(ctx context.Context, addr, certFile, keyFile string, handler http.Handler) error

ListenAndServeTLSContext is a wrapper around http.ListenAndServeTLS() that additionally shuts down the HTTP server gracefully when the context expires, or if an error occurs.

func NewTransport

func NewTransport(opts TransportOpts) (*http.Transport, error)

NewTransport builds an *http.Transport based on the provided options. If no special options are set, this will return an instance that is functionally equivalent to the default settings of http.DefaultTransport.

This function should be preferred over constructing a http.Transport by hand, because it will account for new fields being added to http.Transport over time. For example, http.Transport literals that were written before Go 1.23 will be missing the IdleConnTimeout field, thus setting it to 0 which is equivalent to "leak a ton of memory". This function has test coverage to decrease the chance of this happening... again.

Types

type TransportOpts

type TransportOpts struct {
	// custom tls.Config with fixed server CA cert and/or client cert
	ServerCACertificatePath  string
	ClientCertificatePath    string
	ClientCertificateKeyPath string
}

TransportOpts contains options for building a *http.Transport object.

type WrappedTransport

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

WrappedTransport is a wrapper that adds various global behaviors to an `http.RoundTripper` such as `http.DefaultTransport`.

func WrapTransport

func WrapTransport(transport *http.RoundTripper) *WrappedTransport

WrapTransport replaces the given `http.RoundTripper` with a wrapped version that can be modified using the returned WrappedTransport instance. This usually targets `http.DefaultTransport` like this:

transport := httpext.WrapTransport(&http.DefaultTransport)
transport.SetOverrideUserAgent("example", "1.0")

func (*WrappedTransport) Attach

func (w *WrappedTransport) Attach(wrap func(http.RoundTripper) http.RoundTripper)

Attach adds a custom modifier to the DefaultTransport. The provided function will be used to wrap the existing RoundTripper instance.

func (*WrappedTransport) SetInsecureSkipVerify

func (w *WrappedTransport) SetInsecureSkipVerify(insecure bool)

SetInsecureSkipVerify sets the InsecureSkipVerify flag on the inner Transport's tls.Config. This flag should only be set for testing, esp. to enable capturing of requests made by this application through a tracing proxy like mitmproxy.

func (*WrappedTransport) SetOverrideUserAgent

func (w *WrappedTransport) SetOverrideUserAgent(appName, appVersion string)

SetOverrideUserAgent sets a User-Agent header that will be injected into all HTTP requests that are made with the http.DefaultTransport. The User-Agent string is constructed as "appName/appVersion" from the two provided arguments. The arguments usually come from go-api-declarations/bininfo, for example:

httpext.ModifyDefaultTransport().SetOverrideUserAgent(bininfo.Component(), bininfo.Version())

Jump to

Keyboard shortcuts

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