 Documentation
      ¶
      Documentation
      ¶
    
    
  
    
  
    Overview ¶
Package middleware provides middlewares for htt.Client as RoundTripperHandler
Index ¶
- func BasicAuth(user, passwd string) func(http.RoundTripper) http.RoundTripper
- func CacheWithBody(c *CacheMiddleware)
- func Header(key, value string) func(http.RoundTripper) http.RoundTripper
- func JSON(next http.RoundTripper) http.RoundTripper
- func MaxConcurrent(maxLimit int) func(http.RoundTripper) http.RoundTripper
- type BackoffType
- type CacheEntry
- type CacheMiddleware
- type CacheOption
- type CircuitBreakerFunc
- type CircuitBreakerSvc
- type RepeaterSvc
- type RetryMiddleware
- type RetryOption
- func RetryBufferBodies(enabled bool) RetryOption
- func RetryExcludeCodes(codes ...int) RetryOption
- func RetryMaxBufferSize(size int64) RetryOption
- func RetryMaxDelay(d time.Duration) RetryOption
- func RetryOnCodes(codes ...int) RetryOption
- func RetryWithBackoff(t BackoffType) RetryOption
- func RetryWithJitter(f float64) RetryOption
 
- type RoundTripperFunc
- type RoundTripperHandler
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 ¶
func Header(key, value string) func(http.RoundTripper) http.RoundTripper
Header middleware adds a header to request
func JSON ¶
func JSON(next http.RoundTripper) http.RoundTripper
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
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 ¶
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 ¶
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 ¶
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)
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 ¶
RoundTripperFunc is a functional adapter for RoundTripperHandler
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