Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading