Documentation
¶
Overview ¶
Package middleware provides middleware for Gin
Index ¶
Constants ¶
This section is empty.
Variables ¶
var CircuitBreaker = NewCircuitBreaker()
CircuitBreaker is the cache circuit breaker
var DefaultCircuitBreakerConfig = CircuitBreakerConfig{ FailureThreshold: 5, ResetTimeout: 10 * time.Second, HalfOpenMaxRequests: 3, }
DefaultCircuitBreakerConfig provides sensible defaults for the circuit breaker
var Group = singleflight.Group{}
Group is the global singleflight group for cache requests
Functions ¶
func Cache ¶
func Cache() gin.HandlerFunc
Cache is a middleware that implements Redis caching with singleflight pattern and circuit breaker for resilience. It works as follows:
- First checks if the circuit breaker allows using Redis (bypasses cache if Redis is failing)
- When a request comes in, it checks if the response is in Redis cache
- If found in cache, it serves the cached response immediately
- If not in cache, it uses singleflight to ensure only ONE database query is made regardless of how many concurrent requests are trying to access the same resource
- All concurrent requests for the same resource wait for the first one to complete
- Once the data is retrieved, it's cached in Redis and returned to all waiting clients
- Redis failures are tracked by the circuit breaker which will temporarily bypass the cache if Redis is experiencing problems
This approach significantly reduces database load under high concurrency while maintaining responsiveness for clients even when Redis is experiencing issues.
Types ¶
type CacheCircuitBreaker ¶ added in v1.6.0
type CacheCircuitBreaker struct {
// contains filtered or unexported fields
}
CacheCircuitBreaker implements a simple circuit breaker pattern for Redis cache
func NewCircuitBreaker ¶ added in v1.6.0
func NewCircuitBreaker() *CacheCircuitBreaker
NewCircuitBreaker creates a new circuit breaker with default configuration
func NewCircuitBreakerWithConfig ¶ added in v1.6.0
func NewCircuitBreakerWithConfig(config CircuitBreakerConfig) *CacheCircuitBreaker
NewCircuitBreakerWithConfig creates a new circuit breaker with the given configuration
func (*CacheCircuitBreaker) AllowRequest ¶ added in v1.6.0
func (cb *CacheCircuitBreaker) AllowRequest() bool
AllowRequest checks if a request should use Redis cache or bypass it
func (*CacheCircuitBreaker) RecordFailure ¶ added in v1.6.0
func (cb *CacheCircuitBreaker) RecordFailure()
RecordFailure records a failed Redis operation
func (*CacheCircuitBreaker) RecordSuccess ¶ added in v1.6.0
func (cb *CacheCircuitBreaker) RecordSuccess()
RecordSuccess records a successful Redis operation
func (*CacheCircuitBreaker) State ¶ added in v1.6.0
func (cb *CacheCircuitBreaker) State() CircuitBreakerState
State returns the current state of the circuit breaker
type CircuitBreakerConfig ¶ added in v1.6.0
type CircuitBreakerConfig struct {
// FailureThreshold is the number of consecutive failures required to open the circuit
FailureThreshold uint32
// ResetTimeout is the duration the circuit stays open before moving to half-open
ResetTimeout time.Duration
// HalfOpenMaxRequests is the number of requests allowed through when half-open
HalfOpenMaxRequests uint32
}
CircuitBreakerConfig holds the configuration for the circuit breaker
type CircuitBreakerState ¶ added in v1.6.0
type CircuitBreakerState uint32
CircuitBreakerState represents the state of the circuit breaker
const ( // StateClosed means the circuit is closed and requests flow normally StateClosed CircuitBreakerState = iota // StateOpen means the circuit is open and requests bypass cache StateOpen // StateHalfOpen means we're testing if the circuit can be closed again StateHalfOpen // StateTest is a test state used for test coverage StateTest CircuitBreakerState = 99 )