Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache implements LRU caching middleware for HTTP responses Reduces backend load by serving frequently requested content from memory Uses LRU eviction policy when cache reaches maximum size Time Complexity: O(1) for cache operations with hash map and doubly-linked list Space Complexity: O(n) where n is number of cached entries
func NewCache ¶
func NewCache(config config.CacheConfig) *Cache
NewCache creates a new caching middleware with LRU eviction policy Initializes doubly-linked list with dummy head and tail nodes Dummy nodes simplify insertion and removal logic Time Complexity: O(1) - constant time initialisation Space Complexity: O(1) initial, grows to O(maxSize)
func (*Cache) Wrap ¶
Wrap decorates handler with response caching functionality Checks cache before forwarding request, stores response after processing Only caches successful GET requests to avoid caching errors or side effects Time Complexity: O(1) for cache hit, O(n) for cache miss where n is response size Space Complexity: O(1) for cache operations, O(n) for response buffering
type CacheEntry ¶
type CacheEntry struct {
Body []byte // Response body content
Headers http.Header // HTTP response headers
StatusCode int // HTTP status code
ExpiresAt time.Time // Absolute expiration time for TTL
}
CacheEntry represents a cached HTTP response with metadata Stores complete response data including headers and expiration time TTL-based expiration ensures stale data is not served indefinitely
func (*CacheEntry) IsExpired ¶
func (ce *CacheEntry) IsExpired() bool
IsExpired checks if cache entry has exceeded its TTL Used by cache lookup to determine if entry should be evicted Time Complexity: O(1) - simple time comparison Space Complexity: O(1) - no additional allocations
type Middleware ¶
type Middleware interface {
// Wrap decorates an HTTP handler with additional functionality
// Returns a new handler that executes middleware logic before/after the wrapped handler
// This implements the chain of responsibility pattern for request processing
// Time Complexity: O(1) for wrapping, varies by middleware implementation
// Space Complexity: O(1) for handler wrapping, varies by middleware state
Wrap(next http.Handler) http.Handler
}
Middleware defines the interface for HTTP middleware components This interface implements the decorator pattern for request/response processing Middleware can modify requests, responses, or short-circuit the request chain
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter manages rate limiting for HTTP requests Uses token bucket algorithm with client IP-based bucketing Prevents abuse while allowing legitimate burst traffic Time Complexity: O(1) for rate limit checks Space Complexity: O(n) where n is number of unique client IPs
func NewRateLimiter ¶
func NewRateLimiter(config config.RateLimitConfig) *RateLimiter
NewRateLimiter creates rate limiter with specified limits Initializes empty bucket map for lazy client bucket creation Time Complexity: O(1) - constant time initialisation Space Complexity: O(1) initial, grows with unique clients
func (*RateLimiter) Wrap ¶
func (rl *RateLimiter) Wrap(next http.Handler) http.Handler
Wrap decorates handler with rate limiting functionality Extracts client IP and checks against token bucket Returns 429 Too Many Requests if rate limit exceeded Time Complexity: O(1) for rate limit check Space Complexity: O(1) per unique client IP
type TokenBucket ¶
type TokenBucket struct {
// contains filtered or unexported fields
}
TokenBucket implements token bucket algorithm for rate limiting Allows burst traffic up to bucket capacity while maintaining sustained rate Refills tokens at specified rate to prevent resource exhaustion Time Complexity: O(1) for token operations Space Complexity: O(1) per bucket instance
func NewTokenBucket ¶
func NewTokenBucket(capacity, refillRate int) *TokenBucket
NewTokenBucket creates token bucket with specified capacity and refill rate Initializes bucket at full capacity for immediate availability Time Complexity: O(1) - constant time initialisation Space Complexity: O(1) - fixed size structure
func (*TokenBucket) TryConsume ¶
func (tb *TokenBucket) TryConsume(tokens int) bool
TryConsume attempts to consume specified number of tokens Returns true if tokens available, false if rate limit exceeded Refills bucket based on elapsed time since last refill Time Complexity: O(1) - constant time operations Space Complexity: O(1) - no additional allocations