middleware

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package middleware provides middleware for Gin

Index

Constants

This section is empty.

Variables

View Source
var CircuitBreaker = NewCircuitBreaker()

CircuitBreaker is the cache circuit breaker

View Source
var DefaultCircuitBreakerConfig = CircuitBreakerConfig{
	FailureThreshold:    5,
	ResetTimeout:        10 * time.Second,
	HalfOpenMaxRequests: 3,
}

DefaultCircuitBreakerConfig provides sensible defaults for the circuit breaker

Group is the global singleflight group for cache requests

Functions

func Analytics

func Analytics() gin.HandlerFunc

Analytics will log requests in the database

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:

  1. First checks if the circuit breaker allows using Redis (bypasses cache if Redis is failing)
  2. When a request comes in, it checks if the response is in Redis cache
  3. If found in cache, it serves the cached response immediately
  4. 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
  5. All concurrent requests for the same resource wait for the first one to complete
  6. Once the data is retrieved, it's cached in Redis and returned to all waiting clients
  7. 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

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
)

Jump to

Keyboard shortcuts

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