fix(os/gcache): a little memory leak for removed timestamp key (#3779)

This commit is contained in:
John Guo 2024-09-14 11:05:47 +08:00 committed by GitHub
parent e186eab1a5
commit d8b06d056e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 5 deletions

View File

@ -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.
)

View File

@ -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
@ -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.

View File

@ -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 {