httpkit

package
v0.219.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: Apache-2.0 Imports: 15 Imported by: 1

README

httputil

This package provides a set of utilities for working with HTTP requests and responses.

RoundTripperFunc

RoundTripperFunc is a type that allows you to create an HTTP RoundTripper from a function that takes an HTTP request and returns an HTTP response and an error. This is useful for creating custom middleware to be used with the http.Client or the http.Transport.

RetryRoundTripper

RetryRoundTripper is a type that wraps an existing http.RoundTripper and provides a mechanism for retrying failed requests. This is useful for dealing with transient errors that can occur when making HTTP requests.

The RetryRoundTripper retries requests in case of a recoverable error (such as network timeouts or temporary server errors) up to a certain number of times. If the retries exceed the maximum number of retries, the last response and error are returned.

The RetryRoundTripper considers the following errors as retriable:

  • http.ErrHandlerTimeout
  • net.ErrClosed,
  • and timeout errors

The RetryRoundTripper considers HTTP responses with the following status codes as temporary errors:

  • Internal server error
  • Bad gateway
  • Gateway timeout
  • Service unavailable
  • Insufficient storage
  • Too many requests
  • Request timeout

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mount added in v0.187.0

func Mount(mux Multiplexer, pattern string, handler http.Handler)

Mount will help to register a handler on a request multiplexer in both as the concrete path to the handler and as a prefix match. example:

if pattern -> "/something"
registered as "/something" for exact match
registered as "/something/" for prefix match
Example
package main

import (
	"net/http"

	"go.llib.dev/frameless/pkg/httpkit"
)

func main() {
	var (
		apiV0 http.Handler
		webUI http.Handler
		mux   = http.NewServeMux()
	)
	httpkit.Mount(mux, "/api/v0", apiV0)
	httpkit.Mount(mux, "/ui", webUI)
}

func WithMiddleware added in v0.211.0

func WithMiddleware(handler http.Handler, ffns ...MiddlewareFactoryFunc) http.Handler

WithMiddleware will combine an http.Handler with a stack of middleware factory functions. The order in which you pass the MiddlewareFactoryFunc -s is the same as the order, they will be called during the http.Handler.ServeHTTP method call.

func WithRoundTripper added in v0.216.0

func WithRoundTripper(transport http.RoundTripper, rts ...RoundTripperFactoryFunc) http.RoundTripper

WithRoundTripper will combine an http.RoundTripper with a stack of middleware factory functions. The execution order is in which you pass the factory funcs.

Example
package main

import (
	"net/http"

	"go.llib.dev/frameless/pkg/httpkit"
)

func main() {

	transport := httpkit.WithRoundTripper(nil, func(next http.RoundTripper) http.RoundTripper {
		return httpkit.RoundTripperFunc(func(request *http.Request) (*http.Response, error) {
			request.Header.Set("Authorization", "<type> <credentials>")

			return next.RoundTrip(request)
		})
	})

	_ = &http.Client{
		Transport: transport,
	}

}

Types

type AccessLog

type AccessLog struct {
	Next http.Handler

	AdditionalLoggingDetail func(w http.ResponseWriter, r *http.Request) logging.Detail
}

func (AccessLog) ServeHTTP

func (mw AccessLog) ServeHTTP(w http.ResponseWriter, r *http.Request)

type MiddlewareFactoryFunc added in v0.211.0

type MiddlewareFactoryFunc func(next http.Handler) http.Handler

MiddlewareFactoryFunc is a constructor function that is meant to wrap an http.Handler with given middleware. Its http.Handler argument represents the next middleware http.Handler in the pipeline.

type Multiplexer added in v0.211.0

type Multiplexer interface {
	Handle(pattern string, handler http.Handler)
}

Multiplexer represents a http request Multiplexer.

type RetryRoundTripper

type RetryRoundTripper struct {
	// Transport specifies the mechanism by which individual
	// HTTP requests are made.
	//
	// Default: http.DefaultTransport
	Transport http.RoundTripper
	// RetryStrategy will be used to evaluate if a new retry attempt should be done.
	//
	// Default: retry.ExponentialBackoff
	RetryStrategy retry.Strategy[retry.FailureCount]
	// OnStatus is an [OPTIONAL] configuration field that could contain whether a certain http status code should be retried or not.
	// The RetryRoundTripper has a default behaviour about which status code can be retried, and this option can override that.
	OnStatus map[int]bool
}
Example
package main

import (
	"net/http"
	"time"

	"go.llib.dev/frameless/pkg/httpkit"
	"go.llib.dev/frameless/pkg/retry"
)

func main() {
	httpClient := http.Client{
		Transport: httpkit.RetryRoundTripper{
			RetryStrategy: retry.ExponentialBackoff{ // optional
				Timeout: 5 * time.Minute,
			},

			Transport: http.DefaultTransport, // optional

			OnStatus: map[int]bool{ // optional
				http.StatusTeapot:          true,
				http.StatusTooManyRequests: false,
			},
		},
	}

	httpClient.Get("https://go.llib.dev")
}

func (RetryRoundTripper) RoundTrip

func (rt RetryRoundTripper) RoundTrip(request *http.Request) (resp *http.Response, err error)

RoundTrip

TODO: optional waiting based on the Retry-After header

type RoundTripperFactoryFunc added in v0.216.0

type RoundTripperFactoryFunc func(next http.RoundTripper) http.RoundTripper

RoundTripperFactoryFunc is a constructor function that is meant to wrap an http.RoundTripper with given middleware. Its http.RoundTripper argument represents the next middleware http.RoundTripper in the pipeline.

type RoundTripperFunc

type RoundTripperFunc func(request *http.Request) (*http.Response, error)

func (RoundTripperFunc) RoundTrip

func (fn RoundTripperFunc) RoundTrip(request *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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