roundtripware

package
v0.17.3 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 23 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCircuitBreaker is returned when the request failed because the circuit breaker did not let it go to the next
	// RoundTripware. It wraps the two gobreaker errors (ErrTooManyRequests & ErrOpenState) so only one comparison is
	// needed
	ErrCircuitBreaker = errors.New("circuit breaker triggered")

	// ErrIgnoreSuccessfulness can be returned by the IsSuccessful callback in order for the RoundTripware to ignore the
	// result of the function
	ErrIgnoreSuccessfulness = errors.New("ignored successfulness")

	// ErrReadFromActualBody when it is attempted to read from a body in the IsSuccessful callback that has not
	// previously been copied.
	ErrReadFromActualBody = errors.New("read from actual body")
)

Functions

func LabelerFromContext added in v0.9.12

func LabelerFromContext(ctx context.Context) (context.Context, *otelhttp.Labeler)

LabelerFromContext retrieves a Labeler instance from the provided context if one is available. If no Labeler was found in the provided context a new, empty Labeler is returned and the second return value is false. In this case it is safe to use the Labeler but any attributes added to it will not be used.

Types

type CircuitBreakerOption added in v0.11.8

type CircuitBreakerOption func(opts *CircuitBreakerOptions)

func CircuitBreakerWithIsSuccessful added in v0.11.8

func CircuitBreakerWithIsSuccessful(
	isSuccessful func(err error, req *http.Request, resp *http.Response) error,
	copyReqBody bool,
	copyRespBody bool,
) CircuitBreakerOption

func CircuitBreakerWithMetric added in v0.11.8

func CircuitBreakerWithMetric(
	meter metric.Meter,
	meterName string,
	meterDescription string,
) CircuitBreakerOption

CircuitBreakerWithMetric adds a metric that counts the (un-)successful requests

type CircuitBreakerOptions added in v0.11.8

type CircuitBreakerOptions struct {
	Counter syncint64.Counter

	IsSuccessful func(err error, req *http.Request, resp *http.Response) error
	CopyReqBody  bool
	CopyRespBody bool
}

func NewDefaultCircuitBreakerOptions added in v0.11.8

func NewDefaultCircuitBreakerOptions() *CircuitBreakerOptions

type CircuitBreakerSettings added in v0.11.8

type CircuitBreakerSettings struct {
	// Name is the name of the CircuitBreaker.
	Name string
	// MaxRequests is the maximum number of requests allowed to pass through
	// when the CircuitBreaker is half-open.
	// If MaxRequests is 0, the CircuitBreaker allows only 1 request.
	MaxRequests uint32
	// Interval is the cyclic period of the closed state
	// for the CircuitBreaker to clear the internal Counts.
	// If Interval is less than or equal to 0, the CircuitBreaker doesn't clear internal Counts during the closed state.
	Interval time.Duration
	// Timeout is the period of the open state,
	// after which the state of the CircuitBreaker becomes half-open.
	// If Timeout is less than or equal to 0, the timeout value of the CircuitBreaker is set to 60 seconds.
	Timeout time.Duration
	// ReadyToTrip is called with a copy of Counts whenever a request fails in the closed state.
	// If ReadyToTrip returns true, the CircuitBreaker will be placed into the open state.
	// If ReadyToTrip is nil, default ReadyToTrip is used.
	// Default ReadyToTrip returns true when the number of consecutive failures is more than 5.
	ReadyToTrip func(counts gobreaker.Counts) bool
	// OnStateChange is called whenever the state of the CircuitBreaker changes.
	OnStateChange func(name string, from gobreaker.State, to gobreaker.State)
}

CircuitBreakerSettings is a copy of the gobreaker.Settings, except that the IsSuccessful function is omitted since we want to allow access to the request and response. See `CircuitBreakerWithIsSuccessful` for more.

type Handler

type Handler func(r *http.Request) (*http.Response, error)

type LoggerOption added in v0.11.8

type LoggerOption func(*LoggerOptions)

func LoggerWithErrorMessage added in v0.11.8

func LoggerWithErrorMessage(v string) LoggerOption

LoggerWithErrorMessage middleware option

func LoggerWithMessage added in v0.11.8

func LoggerWithMessage(v string) LoggerOption

LoggerWithMessage middleware option

func LoggerWithMinErrorCode added in v0.11.8

func LoggerWithMinErrorCode(v int) LoggerOption

LoggerWithMinErrorCode middleware option

func LoggerWithMinWarnCode added in v0.11.8

func LoggerWithMinWarnCode(v int) LoggerOption

LoggerWithMinWarnCode middleware option

type LoggerOptions added in v0.11.8

type LoggerOptions struct {
	Message      string
	ErrorMessage string
	MinWarnCode  int
	MinErrorCode int
}

func GetDefaultLoggerOptions added in v0.11.8

func GetDefaultLoggerOptions() LoggerOptions

GetDefaultLoggerOptions returns the default options

type RecoverOption

type RecoverOption func(options *RecoverOptions)

func RecoverWithDisablePrintStack

func RecoverWithDisablePrintStack(v bool) RecoverOption

RecoverWithDisablePrintStack roundTripware option

type RecoverOptions

type RecoverOptions struct {
	DisablePrintStack bool
}

func GetDefaultRecoverOptions

func GetDefaultRecoverOptions() RecoverOptions

GetDefaultRecoverOptions returns the default options

type RefererOption added in v0.14.3

type RefererOption func(*RefererOptions)

func RefererWithHeader added in v0.14.3

func RefererWithHeader(v string) RefererOption

RefererWithHeader middleware option

type RefererOptions added in v0.14.3

type RefererOptions struct {
	Header string
}

func GetDefaultRefererOptions added in v0.14.3

func GetDefaultRefererOptions() RefererOptions

GetDefaultRefererOptions returns the default options

type RequestIDOption added in v0.11.3

type RequestIDOption func(*RequestIDOptions)

func RequestIDWithHeader added in v0.11.3

func RequestIDWithHeader(v string) RequestIDOption

RequestIDWithHeader middleware option

func RequestIDWithProvider added in v0.11.4

func RequestIDWithProvider(v provider.RequestID) RequestIDOption

RequestIDWithProvider middleware option

type RequestIDOptions added in v0.11.3

type RequestIDOptions struct {
	Header    string
	Provider  provider.RequestID
	SetHeader bool
}

func GetDefaultRequestIDOptions added in v0.11.3

func GetDefaultRequestIDOptions() RequestIDOptions

GetDefaultRequestIDOptions returns the default options

type RetryHandler added in v0.13.0

type RetryHandler func(*http.Response) error

type RetryOption added in v0.13.0

type RetryOption func(*RetryOptions)

func RetryWithAttempts added in v0.13.0

func RetryWithAttempts(v uint) RetryOption

func RetryWithContext added in v0.13.0

func RetryWithContext(ctx context.Context) RetryOption

func RetryWithDelay added in v0.13.0

func RetryWithDelay(delay time.Duration) RetryOption

func RetryWithDelayType added in v0.13.0

func RetryWithDelayType(delayType retry.DelayTypeFunc) RetryOption

func RetryWithLastErrorOnly added in v0.13.0

func RetryWithLastErrorOnly(lastErrorOnly bool) RetryOption

func RetryWithMaxDelay added in v0.13.0

func RetryWithMaxDelay(maxDelay time.Duration) RetryOption

func RetryWithOnRetry added in v0.13.0

func RetryWithOnRetry(onRetry retry.OnRetryFunc) RetryOption

func RetryWithRetryIf added in v0.13.0

func RetryWithRetryIf(retryIf retry.RetryIfFunc) RetryOption

type RetryOptions added in v0.13.0

type RetryOptions struct {
	Handler RetryHandler
	// contains filtered or unexported fields
}

func GetDefaultRetryOptions added in v0.13.0

func GetDefaultRetryOptions() RetryOptions

type RoundTripper

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

func NewRoundTripper

func NewRoundTripper(l *zap.Logger, parent http.RoundTripper, roundTripwares ...RoundTripware) *RoundTripper

func (*RoundTripper) RoundTrip

func (rt *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

type RoundTripware

type RoundTripware func(l *zap.Logger, next Handler) Handler

func CircuitBreaker added in v0.11.8

func CircuitBreaker(set *CircuitBreakerSettings, opts ...CircuitBreakerOption) RoundTripware

CircuitBreaker returns a RoundTripper which wraps all the following RoundTripwares and the Handler with a circuit breaker. This will prevent further request once a certain number of requests failed. NOTE: It's strongly advised to add this Roundripware before the metric middleware (if both are used). As the measure- ments of the execution time will otherwise be falsified

func Dump added in v0.9.1

func Dump() RoundTripware

Dump returns a RoundTripper which prints out the request & response object

func DumpRequest

func DumpRequest() RoundTripware

DumpRequest returns a RoundTripper which prints out the request object

func DumpResponse

func DumpResponse() RoundTripware

DumpResponse returns a RoundTripper which prints out the response object

func Logger

func Logger(opts ...LoggerOption) RoundTripware

Logger returns a RoundTripware which logs all requests

func Metric added in v0.9.12

func Metric(meter metric.Meter, name, description string) RoundTripware

Metric returns a RoundTripper which prints out the request & response object

func Recover

func Recover(opts ...RecoverOption) RoundTripware

Recover returns a RoundTripper which catches any panics

func RecoverWithOptions

func RecoverWithOptions(opts RecoverOptions) RoundTripware

RecoverWithOptions returns a RoundTripper which catches any panics

func Referer added in v0.14.3

func Referer(opts ...RefererOption) RoundTripware

Referer returns a RoundTripper which prints out the request & response object

func RequestID added in v0.11.3

func RequestID(opts ...RequestIDOption) RoundTripware

RequestID returns a RoundTripper which prints out the request & response object

func Retry added in v0.13.0

func Retry(opts ...RetryOption) RoundTripware

Retry returns a RoundTripper which retries failed requests

func SessionID added in v0.11.3

func SessionID(opts ...SessionIDOption) RoundTripware

SessionID returns a RoundTripper which prints out the request & response object

func TrackingID added in v0.11.3

func TrackingID(opts ...TrackingIDOption) RoundTripware

TrackingID returns a RoundTripper which prints out the request & response object

type SessionIDGenerator added in v0.11.3

type SessionIDGenerator func() string

type SessionIDOption added in v0.11.3

type SessionIDOption func(*SessionIDOptions)

func SessionIDWithHeader added in v0.11.3

func SessionIDWithHeader(v string) SessionIDOption

SessionIDWithHeader middleware option

type SessionIDOptions added in v0.11.3

type SessionIDOptions struct {
	Header string
}

func GetDefaultSessionIDOptions added in v0.11.3

func GetDefaultSessionIDOptions() SessionIDOptions

GetDefaultSessionIDOptions returns the default options

type TrackingIDGenerator added in v0.11.3

type TrackingIDGenerator func() string

type TrackingIDOption added in v0.11.3

type TrackingIDOption func(*TrackingIDOptions)

func TrackingIDWithHeader added in v0.11.3

func TrackingIDWithHeader(v string) TrackingIDOption

TrackingIDWithHeader middleware option

type TrackingIDOptions added in v0.11.3

type TrackingIDOptions struct {
	Header string
}

func GetDefaultTrackingIDOptions added in v0.11.3

func GetDefaultTrackingIDOptions() TrackingIDOptions

GetDefaultTrackingIDOptions returns the default options

Jump to

Keyboard shortcuts

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