From 02183f557f8460aa9170407748702b4bf746a7e4 Mon Sep 17 00:00:00 2001 From: Artem Poltorzhitskiy Date: Wed, 3 Jul 2024 12:57:20 +0200 Subject: [PATCH] Fix: unlock mutex if panic occurred (#237) --- cmd/api/cache/ttl.go | 71 ++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/cmd/api/cache/ttl.go b/cmd/api/cache/ttl.go index 4e42e37e..17efa40f 100644 --- a/cmd/api/cache/ttl.go +++ b/cmd/api/cache/ttl.go @@ -40,26 +40,26 @@ func (c *TTLCache) Get(key string) ([]byte, bool) { } if time.Now().After(item.expiredAt) { c.mx.Lock() - { - copying := false - if len(c.queue) > len(c.m) { - for i := len(c.queue) - 1; i > 0; i-- { - if copying = copying || c.queue[i] == key; copying { - c.queue[i] = c.queue[i-1] - } + defer c.mx.Unlock() + + copying := false + if len(c.queue) > len(c.m) { + for i := len(c.queue) - 1; i > 0; i-- { + if copying = copying || c.queue[i] == key; copying { + c.queue[i] = c.queue[i-1] } - c.queue[0] = "" - } else { - for i := 0; i < len(c.queue)-1; i++ { - if copying = copying || c.queue[i] == key; copying { - c.queue[i] = c.queue[i+1] - } + } + c.queue[0] = "" + } else { + for i := 0; i < len(c.queue)-1; i++ { + if copying = copying || c.queue[i] == key; copying { + c.queue[i] = c.queue[i+1] } - c.queue[len(c.queue)-1] = "" } - delete(c.m, key) + c.queue[len(c.queue)-1] = "" } - c.mx.Unlock() + delete(c.m, key) + return nil, false } return item.data, true @@ -67,30 +67,29 @@ func (c *TTLCache) Get(key string) ([]byte, bool) { func (c *TTLCache) Set(key string, data []byte) { c.mx.Lock() - { - queueIdx := len(c.m) - item := &ttlItem{ - data: data, - expiredAt: time.Now().Add(c.expiration), - } + defer c.mx.Unlock() - if _, ok := c.m[key]; ok { - c.m[key] = item - } else { - c.m[key] = item - if queueIdx == c.maxEntitiesCount { - keyForRemove := c.queue[0] - for i := 0; i < len(c.queue)-1; i++ { - c.queue[i] = c.queue[i+1] - } - c.queue[queueIdx-1] = key - delete(c.m, keyForRemove) - } else { - c.queue[c.maxEntitiesCount-queueIdx-1] = key + queueIdx := len(c.m) + item := &ttlItem{ + data: data, + expiredAt: time.Now().Add(c.expiration), + } + + if _, ok := c.m[key]; ok { + c.m[key] = item + } else { + c.m[key] = item + if queueIdx == c.maxEntitiesCount { + keyForRemove := c.queue[0] + for i := 0; i < len(c.queue)-1; i++ { + c.queue[i] = c.queue[i+1] } + c.queue[queueIdx-1] = key + delete(c.m, keyForRemove) + } else { + c.queue[c.maxEntitiesCount-queueIdx-1] = key } } - c.mx.Unlock() } func (c *TTLCache) Clear() {