httputils

package
v0.0.0-...-2887efe Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: BSD-3-Clause Imports: 22 Imported by: 3

Documentation

Index

Constants

View Source
const (
	DIAL_TIMEOUT    = time.Minute
	REQUEST_TIMEOUT = 5 * time.Minute

	FAST_DIAL_TIMEOUT    = 50 * time.Millisecond
	FAST_REQUEST_TIMEOUT = 100 * time.Millisecond

	// Exponential backoff defaults.
	INITIAL_INTERVAL     = 500 * time.Millisecond
	RANDOMIZATION_FACTOR = 0.5
	BACKOFF_MULTIPLIER   = 1.5
	MAX_INTERVAL         = 60 * time.Second
	MAX_ELAPSED_TIME     = 5 * time.Minute

	MAX_BYTES_IN_RESPONSE_BODY = 10 * 1024 //10 KB

	// SCHEME_AT_LOAD_BALANCER_HEADER is the header, added by the load balancer,
	// the has the scheme [http|https] that the original request was made under.
	SCHEME_AT_LOAD_BALANCER_HEADER = "x-forwarded-proto"
)

Variables

This section is empty.

Functions

func AddMetricsToClient

func AddMetricsToClient(c *http.Client) *http.Client

AddMetricsToClient adds metrics for each request to the http.Client.

func AddPprofHandlers

func AddPprofHandlers(router *chi.Mux)

AddPprofHandlers adds pprof handlers to the given chi.Mux.

func ConfiguredDialTimeout

func ConfiguredDialTimeout(timeout time.Duration) func(string, string) (net.Conn, error)

ConfiguredDialTimeout is a dialer that sets a given timeout.

func CorsHandler

func CorsHandler(h func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request)

CorsHandler is an HTTP handler function which adds the necessary header for CORS.

func CrossOriginEmbedderPolicy

func CrossOriginEmbedderPolicy(h http.Handler) http.Handler

CrossOriginEmbedderPolicy adds a Cross-Origin-Embedder-Policy: require-corp to every response.

Example:

if !*local {
  h := httputils.CrossOriginEmbedderPolicy(h)
}
http.Handle("/", h)

func CrossOriginOpenerPolicy

func CrossOriginOpenerPolicy(h http.Handler) http.Handler

CrossOriginOpenerPolicy adds a Cross-Origin-Opener-Policy: same-origin to every response.

Example:

if !*local {
  h := httputils.CrossOriginOpenerPolicy(h)
}
http.Handle("/", h)

func CrossOriginResourcePolicy

func CrossOriginResourcePolicy(h http.Handler) http.Handler

CrossOriginResourcePolicy adds a Cross-Origin-Resource-Policy: cross-origin to every response.

Example:

if !*local {
  h := httputils.CrossOriginResourcePolicy(h)
}
http.Handle("/", h)

func DialTimeout

func DialTimeout(network, addr string) (net.Conn, error)

DialTimeout is a dialer that sets a timeout.

func FastDialTimeout

func FastDialTimeout(network, addr string) (net.Conn, error)

FastDialTimeout is a dialer that sets a timeout.

func GetWithContext

func GetWithContext(ctx context.Context, c *http.Client, url string) (*http.Response, error)

GetWithContext is a helper function to execute a GET request to the given url using the given client and the provided context.

func GzipRequestResponse

func GzipRequestResponse(h http.Handler) http.Handler

GzipRequestResponse is middleware that gzips the response.

func HTTPS

func HTTPS(h http.Handler) http.Handler

HTTPS forces traffic to go over HTTPS. See: https://github.com/kubernetes/ingress-gce#redirecting-http-to-https

h - The http.Handler to wrap.

Example:

if !*local {
  h := httputils.HTTPS(h)
}
http.Handle("/", h)

func HealthCheckHandler

func HealthCheckHandler(w http.ResponseWriter, r *http.Request)

HealthCheckHandler returns 200 OK with an empty body, appropriate for a healtcheck endpoint.

func Healthz

func Healthz(h http.Handler) http.Handler

Healthz handles healthchecks at /healthz and GFE healthchecks at /.

Example:

if !*local {
  h := httputils.Healthz(h)
}
http.Handle("/", h)

func HealthzAndHTTPS

func HealthzAndHTTPS(h http.Handler) http.Handler

HealthzAndHTTPS forces traffic to go over HTTPS and also handles healthchecks at /healthz. See: https://github.com/kubernetes/ingress-gce#redirecting-http-to-https

h - The http.Handler to wrap.

Example:

if !*local {
  h := httputils.HealthzAndHTTPS(h)
}
http.Handle("/", h)

func LoggingGzipRequestResponse

func LoggingGzipRequestResponse(h http.Handler) http.Handler

LoggingGzipRequestResponse records parts of the request and the response to the logs and gzips responses when appropriate.

func LoggingRequestResponse

func LoggingRequestResponse(h http.Handler) http.Handler

LoggingRequestResponse records parts of the request and the response to the logs.

func MakeResourceHandler

func MakeResourceHandler(resourcesDir string) func(http.ResponseWriter, *http.Request)

MakeResourceHandler is an HTTP handler function designed for serving files.

func NewConfiguredBackOffTransport

func NewConfiguredBackOffTransport(config *BackOffConfig, base http.RoundTripper) http.RoundTripper

NewConfiguredBackOffTransport creates a BackOffTransport with the specified config, wrapping the given base RoundTripper.

Example: The default retry_interval is .5 seconds, default randomization_factor is 0.5, default multiplier is 1.5 and the default max_interval is 1 minute. For 10 tries the sequence will be (values in seconds) and assuming we go over the max_elapsed_time on the 10th try:

request#     retry_interval     randomized_interval
1             0.5                [0.25,   0.75]
2             0.75               [0.375,  1.125]
3             1.125              [0.562,  1.687]
4             1.687              [0.8435, 2.53]
5             2.53               [1.265,  3.795]
6             3.795              [1.897,  5.692]
7             5.692              [2.846,  8.538]
8             8.538              [4.269, 12.807]
9            12.807              [6.403, 19.210]
10           19.210              backoff.Stop

func NewConfiguredTimeoutClient

func NewConfiguredTimeoutClient(dialTimeout, reqTimeout time.Duration) *http.Client

NewConfiguredTimeoutClient creates a new http.Client with both a dial timeout and a request timeout.

func NewFastTimeoutClient

func NewFastTimeoutClient() *http.Client

NewFastTimeoutClient creates a new http.Client with both a dial timeout and a request timeout.

func NewMetricsTransport

func NewMetricsTransport(rt http.RoundTripper) http.RoundTripper

NewMetricsTransport returns a MetricsTransport instance which wraps the given http.RoundTripper.

func NewTimeoutClient

func NewTimeoutClient() *http.Client

NewTimeoutClient creates a new http.Client with both a dial timeout and a request timeout.

func PaginationParams

func PaginationParams(query url.Values, defaultOffset, defaultSize, maxSize int) (int, int, error)

PaginationParams is helper function to extract pagination parameters from a URL query string. It assumes that 'offset' and 'size' are the query parameters used for pagination. It parses the values and returns an error if they are not integers. If the params are not set the defaults are proviced. Further it ensures that size is never above max size.

func PostWithContext

func PostWithContext(ctx context.Context, c *http.Client, url, contentType string, body io.Reader) (*http.Response, error)

PostWithContext is a helper function to execute a POST request to the given url using the given client and the provided context, contentType and body.

func ReadAndClose

func ReadAndClose(r io.ReadCloser) string

ReadAndClose reads the content of a ReadCloser (e.g. http Response), and returns it as a string. If the response was nil or there was a problem, it will return empty string. The reader, if non-null, will be closed by this function.

func ReadyHandleFunc

func ReadyHandleFunc(w http.ResponseWriter, r *http.Request)

ReadyHandleFunc can be used to set up a ready-handler used to check whether a service is ready. Simply returns a StatusOK

func ReportError

func ReportError(w http.ResponseWriter, err error, message string, code int)

ReportError formats an HTTP error response and also logs the detailed error message. The message parameter is returned in the HTTP response. If it is not provided then "Unknown error" will be returned instead.

func Response2xxAnd3xx

func Response2xxAnd3xx(client *http.Client) *http.Client

Response2xxAnd3xx modifies client so that non-2xx HTTP responses cause a non- nil error return value.

func Response2xxOnly

func Response2xxOnly(client *http.Client) *http.Client

Response2xxOnly modifies client so that non-2xx HTTP responses cause a non-nil error return value.

func RunHealthCheckServer

func RunHealthCheckServer(port string)

RunHealthCheckServer is a helper function which runs an HTTP server which only handles health checks. This is used for processes which don't run an HTTP server of their own but still want health checks. Does not return.

func ServePprof

func ServePprof(port string)

ServePprof starts a web server to serve pprof endpoints on the given port. Does not return.

func XFrameOptionsDeny

func XFrameOptionsDeny(h http.Handler) http.Handler

XFrameOptionsDeny adds "X-Frame-Options: DENY" to every response.

Types

type BackOffConfig

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

func DefaultBackOffConfig

func DefaultBackOffConfig() *BackOffConfig

type BackOffTransport

type BackOffTransport struct {
	Transport http.RoundTripper
	// contains filtered or unexported fields
}

func (*BackOffTransport) RoundTrip

func (t *BackOffTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

type ClientConfig

type ClientConfig struct {
	// DialTimeout, if non-zero, sets the http.Transport's dialer to a net.DialTimeout with the
	// specified timeout.
	DialTimeout time.Duration

	// RequestTimeout, if non-zero, sets the http.Client.Timeout. The timeout applies until the
	// response body is fully read. See more details in the docs for http.Client.Timeout.
	RequestTimeout time.Duration

	// Retries, if non-nil, uses a BackOffTransport to automatically retry requests until receiving a
	// non-5xx response, as specified by the BackOffConfig. See more details in the docs for
	// NewConfiguredBackOffTransport.
	Retries *BackOffConfig

	// TokenSource, if non-nil, uses a oauth2.Transport to authenticate all requests with the
	// specified TokenSource. See auth package for functions to create a TokenSource.
	TokenSource oauth2.TokenSource

	// Response2xxOnly, if true, transforms non-2xx HTTP responses to an error return value.
	Response2xxOnly bool

	// Response2xxAnd3xx, if true, transforms non-2xx and 3xx HTTP responses to
	// an error return value.
	Response2xxAnd3xx bool

	// Metrics, if true, logs each request to metrics.
	Metrics bool
}

ClientConfig represents options for the behavior of an http.Client. Each field, when set, modifies the default http.Client behavior.

Example: client := DefaultClientConfig().WithoutRetries().Client()

func DefaultClientConfig

func DefaultClientConfig() ClientConfig

DefaultClientConfig returns a ClientConfig with reasonable defaults.

  • Timeouts are DIAL_TIMEOUT and REQUEST_TIMEOUT.
  • Retries are enabled with the values from DefaultBackOffConfig().
  • Non-2xx responses are not considered errors.
  • Metrics are enabled.

func (ClientConfig) Client

func (c ClientConfig) Client() *http.Client

Client returns a new http.Client as configured by the ClientConfig.

func (ClientConfig) With2xxAnd3xx

func (c ClientConfig) With2xxAnd3xx() ClientConfig

With2xxAnd3xx returns a new ClientConfig where non-2xx and 3xx responses cause an error.

func (ClientConfig) With2xxOnly

func (c ClientConfig) With2xxOnly() ClientConfig

With2xxOnly returns a new ClientConfig where non-2xx responses cause an error.

func (ClientConfig) WithDialTimeout

func (c ClientConfig) WithDialTimeout(dialTimeout time.Duration) ClientConfig

WithDialTimeout returns a new ClientConfig with the DialTimeout set as specified.

func (ClientConfig) WithRetry4XX

func (c ClientConfig) WithRetry4XX() ClientConfig

WithRetry4XX sets the backoff config to retry all 4XX status codes. This automatically sets the default backoff config if not set.

func (ClientConfig) WithTokenSource

func (c ClientConfig) WithTokenSource(t oauth2.TokenSource) ClientConfig

WithTokenSource returns a new ClientConfig where requests are authenticated with the given TokenSource.

func (ClientConfig) WithoutRetries

func (c ClientConfig) WithoutRetries() ClientConfig

WithoutRetries returns a new ClientConfig where requests are not retried.

type MetricsTransport

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

MetricsTransport is an http.RoundTripper which logs each request to metrics.

func (*MetricsTransport) RoundTrip

func (mt *MetricsTransport) RoundTrip(req *http.Request) (*http.Response, error)

See docs for http.RoundTripper.

type Response2xxAnd3xxTransport

type Response2xxAnd3xxTransport struct {
	http.RoundTripper
}

Response2xxAnd3xxTransport is a RoundTripper that transforms non-2xx and 3xx HTTP responses to an error return value. Delegates all requests to the wrapped RoundTripper, which must be non-nil. Add this behavior to an existing client with Response2xxAnd3xx below.

func (Response2xxAnd3xxTransport) RoundTrip

func (t Response2xxAnd3xxTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

type Response2xxOnlyTransport

type Response2xxOnlyTransport struct {
	http.RoundTripper
}

Response2xxOnlyTransport is a RoundTripper that transforms non-2xx HTTP responses to an error return value. Delegates all requests to the wrapped RoundTripper, which must be non-nil. Add this behavior to an existing client with Response2xxOnly below.

func (Response2xxOnlyTransport) RoundTrip

func (t Response2xxOnlyTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

type ResponsePagination

type ResponsePagination struct {
	Offset int `json:"offset"`
	Size   int `json:"size"`
	Total  int `json:"total"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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