mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-12 05:32:36 +00:00
- 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.
114 lines
2.6 KiB
Go
114 lines
2.6 KiB
Go
package libpack_cache_redis
|
|
|
|
import (
|
|
"time"
|
|
|
|
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
|
|
)
|
|
|
|
// CacheWrapper wraps RedisConfig to implement the CacheClient interface
|
|
// without returning errors, for backward compatibility
|
|
type CacheWrapper struct {
|
|
redis *RedisConfig
|
|
logger *libpack_logger.Logger
|
|
}
|
|
|
|
// NewCacheWrapper creates a new cache wrapper
|
|
func NewCacheWrapper(config *RedisConfig, logger *libpack_logger.Logger) *CacheWrapper {
|
|
if logger == nil {
|
|
logger = &libpack_logger.Logger{}
|
|
}
|
|
return &CacheWrapper{
|
|
redis: config,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// Set stores a value with the given TTL
|
|
func (w *CacheWrapper) Set(key string, value []byte, ttl time.Duration) {
|
|
if err := w.redis.Set(key, value, ttl); err != nil {
|
|
w.logger.Error(&libpack_logger.LogMessage{
|
|
Message: "Redis set error",
|
|
Pairs: map[string]any{
|
|
"error": err.Error(),
|
|
"key": key,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
// Get retrieves a value
|
|
func (w *CacheWrapper) Get(key string) ([]byte, bool) {
|
|
value, found, err := w.redis.Get(key)
|
|
if err != nil {
|
|
w.logger.Error(&libpack_logger.LogMessage{
|
|
Message: "Redis get error",
|
|
Pairs: map[string]any{
|
|
"error": err.Error(),
|
|
"key": key,
|
|
},
|
|
})
|
|
return nil, false
|
|
}
|
|
return value, found
|
|
}
|
|
|
|
// Delete removes a key
|
|
func (w *CacheWrapper) Delete(key string) {
|
|
if err := w.redis.Delete(key); err != nil {
|
|
w.logger.Error(&libpack_logger.LogMessage{
|
|
Message: "Redis delete error",
|
|
Pairs: map[string]any{
|
|
"error": err.Error(),
|
|
"key": key,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
// Clear removes all keys
|
|
func (w *CacheWrapper) Clear() {
|
|
if err := w.redis.Clear(); err != nil {
|
|
w.logger.Error(&libpack_logger.LogMessage{
|
|
Message: "Redis clear error",
|
|
Pairs: map[string]any{
|
|
"error": err.Error(),
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
// CountQueries returns the number of queries
|
|
func (w *CacheWrapper) CountQueries() int64 {
|
|
count, err := w.redis.CountQueries()
|
|
if err != nil {
|
|
w.logger.Error(&libpack_logger.LogMessage{
|
|
Message: "Redis count queries error",
|
|
Pairs: map[string]any{
|
|
"error": err.Error(),
|
|
},
|
|
})
|
|
return 0
|
|
}
|
|
return count
|
|
}
|
|
|
|
// GetMemoryUsage returns 0 for Redis (not applicable)
|
|
func (w *CacheWrapper) GetMemoryUsage() int64 {
|
|
return 0
|
|
}
|
|
|
|
// GetMaxMemorySize returns 0 for Redis (not applicable)
|
|
func (w *CacheWrapper) GetMaxMemorySize() int64 {
|
|
return 0
|
|
}
|
|
|
|
// Close releases the underlying Redis client's connection pool at shutdown.
|
|
// Makes CacheWrapper satisfy io.Closer so the cache layer can close it.
|
|
func (w *CacheWrapper) Close() error {
|
|
if w == nil || w.redis == nil {
|
|
return nil
|
|
}
|
|
return w.redis.Close()
|
|
}
|