middleware

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2025 License: MIT Imports: 13 Imported by: 9

Documentation

Overview

Package middleware provides middlewares for htt.Client as RoundTripperHandler

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BasicAuth

func BasicAuth(user, passwd string) func(http.RoundTripper) http.RoundTripper

BasicAuth middleware adds basic auth to request

func CacheWithBody added in v0.3.0

func CacheWithBody(c *CacheMiddleware)

CacheWithBody includes request body in cache key

func Header(key, value string) func(http.RoundTripper) http.RoundTripper

Header middleware adds a header to request

func JSON

JSON sets Content-Type and Accept headers to json

func MaxConcurrent

func MaxConcurrent(maxLimit int) func(http.RoundTripper) http.RoundTripper

MaxConcurrent middleware limits the total concurrency for a given requester

Types

type BackoffType added in v0.3.0

type BackoffType int

BackoffType represents backoff strategy

const (
	// BackoffConstant is a backoff strategy with constant delay
	BackoffConstant BackoffType = iota
	// BackoffLinear is a backoff strategy with linear delay
	BackoffLinear
	// BackoffExponential is a backoff strategy with exponential delay
	BackoffExponential
)

type CacheEntry added in v0.3.0

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

CacheEntry represents a cached response with metadata

type CacheMiddleware added in v0.3.0

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

CacheMiddleware implements in-memory cache for HTTP responses with TTL-based eviction

func (*CacheMiddleware) RoundTrip added in v0.3.0

func (c *CacheMiddleware) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper

type CacheOption added in v0.3.0

type CacheOption func(c *CacheMiddleware)

CacheOption represents cache middleware options

func CacheMethods added in v0.3.0

func CacheMethods(methods ...string) CacheOption

CacheMethods sets which HTTP methods should be cached

func CacheSize added in v0.3.0

func CacheSize(size int) CacheOption

CacheSize sets maximum number of cached entries

func CacheStatuses added in v0.3.0

func CacheStatuses(codes ...int) CacheOption

CacheStatuses sets which response status codes should be cached

func CacheTTL added in v0.3.0

func CacheTTL(ttl time.Duration) CacheOption

CacheTTL sets cache TTL

func CacheWithHeaders added in v0.3.0

func CacheWithHeaders(headers ...string) CacheOption

CacheWithHeaders includes specified headers in cache key

type CircuitBreakerFunc

type CircuitBreakerFunc func(req func() (interface{}, error)) (interface{}, error)

CircuitBreakerFunc is an adapter to allow the use of ordinary functions as CircuitBreakerSvc.

func (CircuitBreakerFunc) Execute

func (c CircuitBreakerFunc) Execute(req func() (interface{}, error)) (interface{}, error)

Execute CircuitBreakerFunc

type CircuitBreakerSvc

type CircuitBreakerSvc interface {
	Execute(req func() (interface{}, error)) (interface{}, error)
}

CircuitBreakerSvc is an interface wrapping any function to send a request with circuit breaker. can be used with github.com/sony/gobreaker or any similar implementations

type RepeaterSvc

type RepeaterSvc interface {
	Do(ctx context.Context, fun func() error, errs ...error) (err error)
}

RepeaterSvc defines repeater interface

type RetryMiddleware added in v0.3.0

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

RetryMiddleware implements a retry mechanism for http requests with configurable backoff strategies. It supports constant, linear and exponential backoff with optional jitter for better load distribution. By default retries on network errors and 5xx responses. Can be configured to retry on specific status codes or to exclude specific codes from retry.

For requests with bodies (POST, PUT, PATCH), the middleware handles body replay: - If req.GetBody is set (automatic for strings.Reader, bytes.Buffer, bytes.Reader), it uses that - If req.GetBody is nil and body buffering is disabled (default), requests won't be retried - If body buffering is enabled with RetryBufferBodies(true), bodies up to maxBufferSize are buffered

Default configuration:

  • 3 attempts
  • Initial delay: 100ms
  • Max delay: 30s
  • Exponential backoff
  • 10% jitter
  • Retries on 5xx status codes
  • Body buffering disabled (preserves streaming, no retries for bodies without GetBody)

func (*RetryMiddleware) RoundTrip added in v0.3.0

func (r *RetryMiddleware) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper

type RetryOption added in v0.3.0

type RetryOption func(r *RetryMiddleware)

RetryOption represents option type for retry middleware

func RetryBufferBodies added in v0.4.0

func RetryBufferBodies(enabled bool) RetryOption

RetryBufferBodies enables or disables automatic body buffering for retries

func RetryExcludeCodes added in v0.3.0

func RetryExcludeCodes(codes ...int) RetryOption

RetryExcludeCodes sets status codes that should not trigger a retry

func RetryMaxBufferSize added in v0.4.0

func RetryMaxBufferSize(size int64) RetryOption

RetryMaxBufferSize sets the maximum size of request bodies that will be buffered

func RetryMaxDelay added in v0.3.0

func RetryMaxDelay(d time.Duration) RetryOption

RetryMaxDelay sets maximum delay between retries

func RetryOnCodes added in v0.3.0

func RetryOnCodes(codes ...int) RetryOption

RetryOnCodes sets status codes that should trigger a retry

func RetryWithBackoff added in v0.3.0

func RetryWithBackoff(t BackoffType) RetryOption

RetryWithBackoff sets backoff strategy

func RetryWithJitter added in v0.3.0

func RetryWithJitter(f float64) RetryOption

RetryWithJitter sets how much randomness to add to delay (0-1.0)

type RoundTripperFunc

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

RoundTripperFunc is a functional adapter for RoundTripperHandler

func (RoundTripperFunc) RoundTrip

func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip adopts function to the type

type RoundTripperHandler

type RoundTripperHandler func(http.RoundTripper) http.RoundTripper

RoundTripperHandler is a type for middleware handler

func Cache added in v0.3.0

func Cache(opts ...CacheOption) RoundTripperHandler

Cache creates caching middleware with provided options

func CircuitBreaker

func CircuitBreaker(svc CircuitBreakerSvc) RoundTripperHandler

CircuitBreaker middleware injects external CircuitBreakerSvc into the call chain

func Repeater

func Repeater(repeater RepeaterSvc, failOnCodes ...int) RoundTripperHandler

Repeater sets middleware with provided RepeaterSvc to retry failed requests

func Retry added in v0.3.0

func Retry(attempts int, initialDelay time.Duration, opts ...RetryOption) RoundTripperHandler

Retry creates retry middleware with provided options

Directories

Path Synopsis
Package cache implements middleware for response caching.
Package cache implements middleware for response caching.
Package logger implements middleware for request logging.
Package logger implements middleware for request logging.

Jump to

Keyboard shortcuts

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