mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-24 06:48:09 +00:00
Fix leaky bytes allocation for cache.
This commit is contained in:
@@ -14,15 +14,13 @@ func calculateHash(c *fiber.Ctx) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func enableCache() {
|
func enableCache() {
|
||||||
cfg.Cache.CacheClient = libpack_cache.New(time.Duration(cfg.Cache.CacheTTL) * time.Second * 2)
|
cfg.Cache.CacheClient = libpack_cache.New(time.Duration(cfg.Cache.CacheTTL) * time.Second * 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cacheLookup(hash string) []byte {
|
func cacheLookup(hash string) []byte {
|
||||||
if cfg.Cache.CacheClient != nil {
|
obj, found := cfg.Cache.CacheClient.Get(hash)
|
||||||
obj, found := cfg.Cache.CacheClient.Get(hash)
|
if found {
|
||||||
if found {
|
return obj
|
||||||
return obj
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+10
-12
@@ -44,23 +44,22 @@ func (c *Cache) cleanupRoutine(globalTTL time.Duration) {
|
|||||||
func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
|
func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
|
expiresAt := time.Now().Add(ttl)
|
||||||
|
|
||||||
now := time.Now()
|
// Get a byte slice from the pool and ensure it's properly sized.
|
||||||
expiresAt := now.Add(ttl)
|
b := c.bytePool.Get().([]byte)
|
||||||
if expiresAt.After(now.Add(c.globalTTL)) {
|
if cap(b) < len(value) {
|
||||||
expiresAt = now.Add(c.globalTTL)
|
b = make([]byte, len(value))
|
||||||
|
} else {
|
||||||
|
b = b[:len(value)]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate a byte slice from the pool.
|
copy(b, value)
|
||||||
|
|
||||||
entry := CacheEntry{
|
entry := CacheEntry{
|
||||||
Value: c.bytePool.Get().([]byte),
|
Value: b,
|
||||||
ExpiresAt: expiresAt,
|
ExpiresAt: expiresAt,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy the value into the byte slice.
|
|
||||||
copy(entry.Value, value)
|
|
||||||
|
|
||||||
// Set the entry in the cache.
|
|
||||||
c.entries.Store(key, entry)
|
c.entries.Store(key, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +75,6 @@ func (c *Cache) Get(key string) ([]byte, bool) {
|
|||||||
// Copy the value from the byte slice.
|
// Copy the value from the byte slice.
|
||||||
value := make([]byte, len(entry.(CacheEntry).Value))
|
value := make([]byte, len(entry.(CacheEntry).Value))
|
||||||
copy(value, entry.(CacheEntry).Value)
|
copy(value, entry.(CacheEntry).Value)
|
||||||
|
|
||||||
return value, true
|
return value, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -27,7 +27,7 @@ func (suite *Tests) Test_cacheLookup() {
|
|||||||
{
|
{
|
||||||
name: "test_existent",
|
name: "test_existent",
|
||||||
args: args{
|
args: args{
|
||||||
hash: "00000000000000000000000000000000000001",
|
hash: "00000000000000000000000000000000001337",
|
||||||
},
|
},
|
||||||
want: []byte("it's fine."),
|
want: []byte("it's fine."),
|
||||||
addCache: struct {
|
addCache: struct {
|
||||||
@@ -40,7 +40,7 @@ func (suite *Tests) Test_cacheLookup() {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
suite.T().Run(tt.name, func(t *testing.T) {
|
suite.T().Run(tt.name, func(t *testing.T) {
|
||||||
if tt.addCache.data != nil {
|
if tt.addCache.data != nil {
|
||||||
cfg.Cache.CacheClient.Set(tt.args.hash, tt.addCache.data, time.Duration(1)*time.Second)
|
cfg.Cache.CacheClient.Set(tt.args.hash, tt.addCache.data, time.Duration(90*time.Second))
|
||||||
}
|
}
|
||||||
got := cacheLookup(tt.args.hash)
|
got := cacheLookup(tt.args.hash)
|
||||||
assert.Equal(tt.want, got, "Unexpected cache lookup result")
|
assert.Equal(tt.want, got, "Unexpected cache lookup result")
|
||||||
|
|||||||
Reference in New Issue
Block a user