Skip to content

Commit

Permalink
fix(os/gcache): a little memory leak for removed timestamp key (#3779)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored Sep 14, 2024
1 parent e186eab commit d8b06d0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion errors/gcode/gcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
CodeNotFound = localCode{65, "Not Found", nil} // Resource does not exist.
CodeInvalidRequest = localCode{66, "Invalid Request", nil} // Invalid request.
CodeNecessaryPackageNotImport = localCode{67, "Necessary Package Not Import", nil} // It needs necessary package import.
CodeInternalPanic = localCode{68, "Internal Panic", nil} // An panic occurred internally.
CodeInternalPanic = localCode{68, "Internal Panic", nil} // A panic occurred internally.
CodeBusinessValidationFailed = localCode{300, "Business Validation Failed", nil} // Business validation failed.
)

Expand Down
5 changes: 3 additions & 2 deletions os/gcache/gcache_adapter_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (c *AdapterMemory) Remove(ctx context.Context, keys ...interface{}) (*gvar.
for _, key := range removedKeys {
c.eventList.PushBack(&adapterMemoryEvent{
k: key,
e: gtime.TimestampMilli() - 1000000,
e: gtime.TimestampMilli() - 1000,
})
}
return gvar.New(value), nil
Expand Down Expand Up @@ -416,12 +416,13 @@ func (c *AdapterMemory) syncEventAndClearExpired(ctx context.Context) {
oldExpireTime = c.expireTimes.Get(event.k)
// Calculating the new expiration time set.
newExpireTime = c.makeExpireKey(event.e)
// Expiration changed for this key.
if newExpireTime != oldExpireTime {
c.expireSets.GetOrNew(newExpireTime).Add(event.k)
if oldExpireTime != 0 {
c.expireSets.GetOrNew(oldExpireTime).Remove(event.k)
}
// Updating the expired time for <event.k>.
// Updating the expired time for `event.k`.
c.expireTimes.Set(event.k, newExpireTime)
}
// Adding the key the LRU history by writing operations.
Expand Down
6 changes: 4 additions & 2 deletions os/gcache/gcache_adapter_memory_expire_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
)

type adapterMemoryExpireSets struct {
mu sync.RWMutex // expireSetMu ensures the concurrent safety of expireSets map.
expireSets map[int64]*gset.Set // expireSets is the expiring timestamp to its key set mapping, which is used for quick indexing and deleting.
// expireSetMu ensures the concurrent safety of expireSets map.
mu sync.RWMutex
// expireSets is the expiring timestamp in seconds to its key set mapping, which is used for quick indexing and deleting.
expireSets map[int64]*gset.Set
}

func newAdapterMemoryExpireSets() *adapterMemoryExpireSets {
Expand Down

0 comments on commit d8b06d0

Please sign in to comment.