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:
2026-08-13 07:16:52 +01:00
parent fc28c65681
commit 14be2398e8
10 changed files with 167 additions and 37 deletions
+29
View File
@@ -183,3 +183,32 @@ func TestSetMaxMemorySize(t *testing.T) {
func generateKey(index int) string {
return "test-key-" + fmt.Sprintf("%d", index)
}
// TestEvictToFreeMemoryWithSmallMaxEntriesReachesTarget is a regression for
// under-eviction: evictToFreeMemory used to cap its candidate set at
// maxCacheSize/5 and stop at the end of that subset. With a small
// maxCacheSize the cap (maxCacheSize/5) could be lower than the number of
// entries needed to satisfy bytesToFree, so the cache stayed over its
// memory limit.
func TestEvictToFreeMemoryWithSmallMaxEntriesReachesTarget(t *testing.T) {
// maxCacheSize=10 -> old candidate cap was maxCacheSize/5 = 2 entries.
cache := NewWithSize(5*time.Second, DefaultMaxMemorySize, 10)
val := make([]byte, 1024) // ~1KB + overhead each
const count = 8
for i := 0; i < count; i++ {
cache.Set(fmt.Sprintf("e%d", i), val, time.Second)
}
before := cache.GetMemoryUsage()
// Request freeing an amount equal to ~half the entries (well beyond the
// old 2-entry cap).
need := (count / 2) * (int64(len(val)) + approxEntryOverhead)
cache.evictToFreeMemory(need)
after := cache.GetMemoryUsage()
assert.LessOrEqual(t, after, before-need,
"evictToFreeMemory must free at least the requested bytes even across more than maxCacheSize/5 entries")
// Requesting more than the total must drain the cache.
cache.evictToFreeMemory(1 << 30)
assert.Equal(t, int64(0), cache.GetMemoryUsage(),
"an oversized request should evict every entry")
}