middleware

package
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CircuitBreakerMiddleware

func CircuitBreakerMiddleware(cb *CircuitBreaker) func(http.RoundTripper) http.RoundTripper

CircuitBreakerMiddleware returns a middleware that implements circuit breaker

func LoggingMiddleware

func LoggingMiddleware(logger Logger) func(http.RoundTripper) http.RoundTripper

func RateLimitMiddleware

func RateLimitMiddleware(limiter *RateLimiter) func(http.RoundTripper) http.RoundTripper

RateLimitMiddleware returns a middleware that enforces rate limiting

func RetryMiddleware

func RetryMiddleware(config RetryConfig) func(http.RoundTripper) http.RoundTripper

RetryMiddleware returns a middleware that retries requests based on the config

Types

type CircuitBreaker

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

CircuitBreaker implements a simple circuit breaker

func NewCircuitBreaker

func NewCircuitBreaker(config CircuitBreakerConfig) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker

func (*CircuitBreaker) Call

func (cb *CircuitBreaker) Call(fn func() (*http.Response, error)) (*http.Response, error)

Call executes the function with circuit breaker logic

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	FailureThreshold int           // Number of failures to open the circuit
	SuccessThreshold int           // Number of successes to close the circuit from half-open
	Timeout          time.Duration // Time to wait before trying half-open
}

CircuitBreakerConfig holds configuration for circuit breaker middleware

func DefaultCircuitBreakerConfig

func DefaultCircuitBreakerConfig() CircuitBreakerConfig

DefaultCircuitBreakerConfig returns a default circuit breaker configuration

type CircuitBreakerState

type CircuitBreakerState int

CircuitBreakerState represents the state of the circuit breaker

const (
	StateClosed CircuitBreakerState = iota
	StateOpen
	StateHalfOpen
)

type Logger added in v1.8.2

type Logger interface {
	Debug(msg string, args ...any)
	Info(msg string, args ...any)
	Warn(msg string, args ...any)
	Error(msg string, args ...any)
}

type RateLimiter

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

RateLimiter implements a basic token bucket rate limiter

func NewRateLimiter

func NewRateLimiter(capacity int, refillRate time.Duration) *RateLimiter

NewRateLimiter creates a new rate limiter

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow() bool

Allow checks if a request is allowed and consumes a token

type RetryConfig

type RetryConfig struct {
	MaxRetries     int
	Backoff        func(attempt int) time.Duration
	RetryCondition func(resp *http.Response, err error) bool
}

RetryConfig holds configuration for retry middleware

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns a default retry configuration

Jump to

Keyboard shortcuts

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