package proxy import ( "crypto/sha256" "encoding/hex" "sync" "time" ) // CacheEntry holds a cached HTTP response. type CacheEntry struct { ContentType string Body []byte StatusCode int ExpiresAt time.Time ETag string } // computeETag computes an HTTP entity tag for a payload. func computeETag(body []byte) string { h := sha256.Sum256(body) return "\"" + hex.EncodeToString(h[:8]) + "\"" } // CacheStats tracks proxy cache operational metrics. type CacheStats struct { Hits uint64 `json:"hits"` Misses uint64 `json:"misses"` Evictions uint64 `json:"evictions"` Items int `json:"items"` } // ResponseCache is a thread-safe, bounded in-memory response cache. type ResponseCache struct { mu sync.RWMutex entries map[string]CacheEntry order []string // simple FIFO order for eviction maxItems int hits uint64 misses uint64 evictions uint64 } // NewResponseCache creates a new ResponseCache with a capacity ceiling. func NewResponseCache(maxItems int) *ResponseCache { if maxItems <= 0 { maxItems = 512 } return &ResponseCache{ entries: make(map[string]CacheEntry, maxItems), order: make([]string, 0, maxItems), maxItems: maxItems, } } // Get retrieves an unexpired entry from cache. func (c *ResponseCache) Get(key string) (CacheEntry, bool) { c.mu.Lock() defer c.mu.Unlock() entry, ok := c.entries[key] if !ok { c.misses++ return CacheEntry{}, false } if time.Now().After(entry.ExpiresAt) { delete(c.entries, key) c.misses++ return CacheEntry{}, false } c.hits++ return entry, true } // Set stores a response entry in the cache, evicting the oldest if full. func (c *ResponseCache) Set(key string, entry CacheEntry) { c.mu.Lock() defer c.mu.Unlock() if _, ok := c.entries[key]; !ok { for len(c.entries) >= c.maxItems && len(c.order) > 0 { oldest := c.order[0] c.order = c.order[1:] delete(c.entries, oldest) c.evictions++ } c.order = append(c.order, key) } c.entries[key] = entry } // Stats returns a snapshot of cache metrics. func (c *ResponseCache) Stats() CacheStats { c.mu.RLock() defer c.mu.RUnlock() return CacheStats{ Hits: c.hits, Misses: c.misses, Evictions: c.evictions, Items: len(c.entries), } } // singleflightCall represents an active or completed in-flight upstream request. type singleflightCall struct { done chan struct{} val any err error } // singleflightGroup coalesces duplicate concurrent requests. type singleflightGroup struct { mu sync.Mutex m map[string]*singleflightCall } func (g *singleflightGroup) Do(key string, fn func() (any, error)) (any, error) { g.mu.Lock() if g.m == nil { g.m = make(map[string]*singleflightCall) } if c, ok := g.m[key]; ok { g.mu.Unlock() <-c.done return c.val, c.err } c := &singleflightCall{ done: make(chan struct{}), } g.m[key] = c g.mu.Unlock() go func() { defer func() { close(c.done) g.mu.Lock() delete(g.m, key) g.mu.Unlock() }() c.val, c.err = fn() }() <-c.done return c.val, c.err }