From 6f4ae8f58df667131fbb17aa333263a53a38b863 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Sun, 6 Aug 2023 10:55:04 +0100 Subject: [PATCH] [bugfix] update cache library with nil ptr fix (#2070) Signed-off-by: kim --- go.mod | 2 +- go.sum | 4 +-- internal/cache/size.go | 3 ++ .../gruf/go-cache/v3/result/cache.go | 36 +++++++++++-------- .../gruf/go-cache/v3/result/pool.go | 1 + vendor/modules.txt | 2 +- 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index 81c7874c9..999068087 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( codeberg.org/gruf/go-bytesize v1.0.2 codeberg.org/gruf/go-byteutil v1.1.2 - codeberg.org/gruf/go-cache/v3 v3.5.5 + codeberg.org/gruf/go-cache/v3 v3.5.6 codeberg.org/gruf/go-debug v1.3.0 codeberg.org/gruf/go-errors/v2 v2.2.0 codeberg.org/gruf/go-fastcopy v1.1.2 diff --git a/go.sum b/go.sum index 4b0d31864..d73cc25cf 100644 --- a/go.sum +++ b/go.sum @@ -48,8 +48,8 @@ codeberg.org/gruf/go-bytesize v1.0.2/go.mod h1:n/GU8HzL9f3UNp/mUKyr1qVmTlj7+xacp codeberg.org/gruf/go-byteutil v1.0.0/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU= codeberg.org/gruf/go-byteutil v1.1.2 h1:TQLZtTxTNca9xEfDIndmo7nBYxeS94nrv/9DS3Nk5Tw= codeberg.org/gruf/go-byteutil v1.1.2/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU= -codeberg.org/gruf/go-cache/v3 v3.5.5 h1:Ce7odyvr8oF6h49LSjPL7AZs2QGyKMN9BPkgKcfR0BA= -codeberg.org/gruf/go-cache/v3 v3.5.5/go.mod h1:NbsGQUgEdNFd631WSasvCHIVAaY9ovuiSeoBwtsIeDc= +codeberg.org/gruf/go-cache/v3 v3.5.6 h1:TJnNOuij5DF/ZK9pDB61SlYzxidRQeYjYYW3dfFSznc= +codeberg.org/gruf/go-cache/v3 v3.5.6/go.mod h1:NbsGQUgEdNFd631WSasvCHIVAaY9ovuiSeoBwtsIeDc= codeberg.org/gruf/go-debug v1.3.0 h1:PIRxQiWUFKtGOGZFdZ3Y0pqyfI0Xr87j224IYe2snZs= codeberg.org/gruf/go-debug v1.3.0/go.mod h1:N+vSy9uJBQgpQcJUqjctvqFz7tBHJf+S/PIjLILzpLg= codeberg.org/gruf/go-errors/v2 v2.0.0/go.mod h1:ZRhbdhvgoUA3Yw6e56kd9Ox984RrvbEFC2pOXyHDJP4= diff --git a/internal/cache/size.go b/internal/cache/size.go index 56524575b..ec7c554c0 100644 --- a/internal/cache/size.go +++ b/internal/cache/size.go @@ -157,12 +157,14 @@ func totalOfRatios() float64 { config.GetCacheAccountNoteMemRatio() + config.GetCacheBlockMemRatio() + config.GetCacheBlockIDsMemRatio() + + config.GetCacheBoostOfIDsMemRatio() + config.GetCacheEmojiMemRatio() + config.GetCacheEmojiCategoryMemRatio() + config.GetCacheFollowMemRatio() + config.GetCacheFollowIDsMemRatio() + config.GetCacheFollowRequestMemRatio() + config.GetCacheFollowRequestIDsMemRatio() + + config.GetCacheInReplyToIDsMemRatio() + config.GetCacheInstanceMemRatio() + config.GetCacheListMemRatio() + config.GetCacheListEntryMemRatio() + @@ -173,6 +175,7 @@ func totalOfRatios() float64 { config.GetCacheReportMemRatio() + config.GetCacheStatusMemRatio() + config.GetCacheStatusFaveMemRatio() + + config.GetCacheStatusFaveIDsMemRatio() + config.GetCacheTagMemRatio() + config.GetCacheTombstoneMemRatio() + config.GetCacheUserMemRatio() + diff --git a/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go b/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go index 665481d55..5756c0cf5 100644 --- a/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go +++ b/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go @@ -91,6 +91,7 @@ func (c *Cache[T]) SetEvictionCallback(hook func(T)) { if res.Error != nil { // Skip value hooks + putResult(res) return } @@ -119,6 +120,7 @@ func (c *Cache[T]) SetInvalidateCallback(hook func(T)) { if res.Error != nil { // Skip value hooks + putResult(res) return } @@ -147,10 +149,8 @@ func (c *Cache[T]) IgnoreErrors(ignore func(error) bool) { // Load will attempt to load an existing result from the cacche for the given lookup and key parts, else calling the provided load function and caching the result. func (c *Cache[T]) Load(lookup string, load func() (T, error), keyParts ...any) (T, error) { - var ( - zero T - res *result - ) + var zero T + var res *result // Get lookup key info by name. keyInfo := c.lookups.get(lookup) @@ -164,12 +164,11 @@ func (c *Cache[T]) Load(lookup string, load func() (T, error), keyParts ...any) // Acquire cache lock c.cache.Lock() - // Look for primary cache key - pkeys := keyInfo.pkeys[ckey] - - if len(pkeys) > 0 { + // Look for primary key for cache key (only accept len=1) + if pkeys := keyInfo.pkeys[ckey]; len(pkeys) == 1 { // Fetch the result for primary key entry, ok := c.cache.Cache.Get(pkeys[0]) + if ok { // Since the invalidation / eviction hooks acquire a mutex // lock separately, and only at this point are the pkeys @@ -298,12 +297,11 @@ func (c *Cache[T]) Has(lookup string, keyParts ...any) bool { // Acquire cache lock c.cache.Lock() - // Look for primary key for cache key - pkeys := keyInfo.pkeys[ckey] - - if len(pkeys) > 0 { + // Look for primary key for cache key (only accept len=1) + if pkeys := keyInfo.pkeys[ckey]; len(pkeys) == 1 { // Fetch the result for primary key entry, ok := c.cache.Cache.Get(pkeys[0]) + if ok { // Since the invalidation / eviction hooks acquire a mutex // lock separately, and only at this point are the pkeys @@ -364,17 +362,25 @@ func (c *Cache[T]) store(res *result) (evict func()) { if key.info.unique && len(pkeys) > 0 { for _, conflict := range pkeys { // Get the overlapping result with this key. - entry, _ := c.cache.Cache.Get(conflict) - confRes := entry.Value.(*result) + entry, ok := c.cache.Cache.Get(conflict) + + if !ok { + // Since the invalidation / eviction hooks acquire a mutex + // lock separately, and only at this point are the pkeys + // updated, there is a chance that a primary key may return + // no matching entry. Hence we have to check for it here. + continue + } // From conflicting entry, drop this key, this // will prevent eviction cleanup key confusion. + confRes := entry.Value.(*result) confRes.Keys.drop(key.info.name) if len(res.Keys) == 0 { // We just over-wrote the only lookup key for // this value, so we drop its primary key too. - c.cache.Cache.Delete(conflict) + _ = c.cache.Cache.Delete(conflict) // Add finished result to evict queue. toEvict = append(toEvict, confRes) diff --git a/vendor/codeberg.org/gruf/go-cache/v3/result/pool.go b/vendor/codeberg.org/gruf/go-cache/v3/result/pool.go index c5cbeda57..6f194be5a 100644 --- a/vendor/codeberg.org/gruf/go-cache/v3/result/pool.go +++ b/vendor/codeberg.org/gruf/go-cache/v3/result/pool.go @@ -17,6 +17,7 @@ func getResult() *result { // putResult replaces a result in the pool. func putResult(r *result) { + r.PKey = 0 r.Keys = nil r.Value = nil r.Error = nil diff --git a/vendor/modules.txt b/vendor/modules.txt index 54425e3d3..63946d3f7 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -13,7 +13,7 @@ codeberg.org/gruf/go-bytesize # codeberg.org/gruf/go-byteutil v1.1.2 ## explicit; go 1.16 codeberg.org/gruf/go-byteutil -# codeberg.org/gruf/go-cache/v3 v3.5.5 +# codeberg.org/gruf/go-cache/v3 v3.5.6 ## explicit; go 1.19 codeberg.org/gruf/go-cache/v3 codeberg.org/gruf/go-cache/v3/result