mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-20 06:28:13 +00:00
fix: restore tracing endpoint validation; honor per-side client timeouts; cache eviction target; redis close
- tracing: validateEndpoint used fmt.Sscanf("%s:%d") which treats %s as
greedy, so every valid host:port endpoint failed and traced==nil disabled
tracing entirely. Validate with net.SplitHostPort (pure helper, port range
0-65535); test TestValidateEndpoint.
- proxy: createFasthttpClient used clientTimeout for every timeout slot,
ignoring the parsed CLIENT_READ_TIMEOUT/CLIENT_WRITE_TIMEOUT (silent
config loss, documented separately in README). Honour per-side read/write
timeouts, falling back to client timeout when unset.
- cache/memory: evictToFreeMemory capped candidates at maxCacheSize/5 and
stopped at the subset end, so a large write could leave the cache over
its memory limit. Collect all entries and sort.Slice (correct + O(n log n)
vs O(cap^2)), evict until the target is met or the cache is drained.
- cache/redis: the Redis cache client was never closed at shutdown (unlike
the metrics aggregator). Add RedisConfig.Close, CacheWrapper.Close, a
cache.Shutdown and register it with the ShutdownManager.
This commit is contained in:
Vendored
+17
-19
@@ -8,6 +8,7 @@ import (
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"io"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
@@ -322,32 +323,29 @@ func (c *Cache) evictToFreeMemory(bytesToFree int64) {
|
||||
memorySize int64
|
||||
}
|
||||
|
||||
// Collect entries to consider for eviction
|
||||
entries := make([]keyMemorySize, 0, int(c.maxCacheSize/5))
|
||||
// Collect ALL entries so eviction can actually reach the target. The
|
||||
// previous code capped the candidate set at maxCacheSize/5 and stopped at
|
||||
// the end of that subset, so a large write could leave the cache over its
|
||||
// memory limit.
|
||||
entries := make([]keyMemorySize, 0, int(c.maxCacheSize))
|
||||
c.entries.Range(func(k, v any) bool {
|
||||
key := k.(string)
|
||||
entry := v.(CacheEntry)
|
||||
entries = append(entries, keyMemorySize{entry.ExpiresAt, key, entry.MemorySize})
|
||||
return len(entries) < cap(entries)
|
||||
return true
|
||||
})
|
||||
|
||||
// Sort entries by expiry time (oldest first)
|
||||
// Simple selection sort since we only need to find the oldest entries
|
||||
var freedBytes int64
|
||||
for i := 0; i < len(entries) && freedBytes < bytesToFree; i++ {
|
||||
oldest := i
|
||||
for j := i + 1; j < len(entries); j++ {
|
||||
if entries[j].expiresAt.Before(entries[oldest].expiresAt) {
|
||||
oldest = j
|
||||
}
|
||||
}
|
||||
// Swap
|
||||
if oldest != i {
|
||||
entries[i], entries[oldest] = entries[oldest], entries[i]
|
||||
}
|
||||
// Sort by expiry time (oldest first)
|
||||
sort.Slice(entries, func(i, j int) bool {
|
||||
return entries[i].expiresAt.Before(entries[j].expiresAt)
|
||||
})
|
||||
|
||||
// Delete this entry
|
||||
if entry, exists := c.entries.LoadAndDelete(entries[i].key); exists {
|
||||
var freedBytes int64
|
||||
for _, e := range entries {
|
||||
if freedBytes >= bytesToFree {
|
||||
break
|
||||
}
|
||||
if entry, exists := c.entries.LoadAndDelete(e.key); exists {
|
||||
cacheEntry := entry.(CacheEntry)
|
||||
atomic.AddInt64(&c.entryCount, -1)
|
||||
atomic.AddInt64(&c.memoryUsage, -cacheEntry.MemorySize)
|
||||
|
||||
Reference in New Issue
Block a user