Skip to content

Commit

Permalink
Fix: unlock mutex if panic occurred (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Jul 3, 2024
1 parent 01b70a2 commit 02183f5
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions cmd/api/cache/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,57 +40,56 @@ 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
}

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() {
Expand Down

0 comments on commit 02183f5

Please sign in to comment.