mirror of
1
Fork 0

[bugfix] update cache library with nil ptr fix (#2070)

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2023-08-06 10:55:04 +01:00 committed by GitHub
parent 7f1f2b80ea
commit 6f4ae8f58d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 19 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.20
require ( require (
codeberg.org/gruf/go-bytesize v1.0.2 codeberg.org/gruf/go-bytesize v1.0.2
codeberg.org/gruf/go-byteutil v1.1.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-debug v1.3.0
codeberg.org/gruf/go-errors/v2 v2.2.0 codeberg.org/gruf/go-errors/v2 v2.2.0
codeberg.org/gruf/go-fastcopy v1.1.2 codeberg.org/gruf/go-fastcopy v1.1.2

4
go.sum
View File

@ -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.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 h1:TQLZtTxTNca9xEfDIndmo7nBYxeS94nrv/9DS3Nk5Tw=
codeberg.org/gruf/go-byteutil v1.1.2/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU= 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.6 h1:TJnNOuij5DF/ZK9pDB61SlYzxidRQeYjYYW3dfFSznc=
codeberg.org/gruf/go-cache/v3 v3.5.5/go.mod h1:NbsGQUgEdNFd631WSasvCHIVAaY9ovuiSeoBwtsIeDc= 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 h1:PIRxQiWUFKtGOGZFdZ3Y0pqyfI0Xr87j224IYe2snZs=
codeberg.org/gruf/go-debug v1.3.0/go.mod h1:N+vSy9uJBQgpQcJUqjctvqFz7tBHJf+S/PIjLILzpLg= 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= codeberg.org/gruf/go-errors/v2 v2.0.0/go.mod h1:ZRhbdhvgoUA3Yw6e56kd9Ox984RrvbEFC2pOXyHDJP4=

View File

@ -157,12 +157,14 @@ func totalOfRatios() float64 {
config.GetCacheAccountNoteMemRatio() + config.GetCacheAccountNoteMemRatio() +
config.GetCacheBlockMemRatio() + config.GetCacheBlockMemRatio() +
config.GetCacheBlockIDsMemRatio() + config.GetCacheBlockIDsMemRatio() +
config.GetCacheBoostOfIDsMemRatio() +
config.GetCacheEmojiMemRatio() + config.GetCacheEmojiMemRatio() +
config.GetCacheEmojiCategoryMemRatio() + config.GetCacheEmojiCategoryMemRatio() +
config.GetCacheFollowMemRatio() + config.GetCacheFollowMemRatio() +
config.GetCacheFollowIDsMemRatio() + config.GetCacheFollowIDsMemRatio() +
config.GetCacheFollowRequestMemRatio() + config.GetCacheFollowRequestMemRatio() +
config.GetCacheFollowRequestIDsMemRatio() + config.GetCacheFollowRequestIDsMemRatio() +
config.GetCacheInReplyToIDsMemRatio() +
config.GetCacheInstanceMemRatio() + config.GetCacheInstanceMemRatio() +
config.GetCacheListMemRatio() + config.GetCacheListMemRatio() +
config.GetCacheListEntryMemRatio() + config.GetCacheListEntryMemRatio() +
@ -173,6 +175,7 @@ func totalOfRatios() float64 {
config.GetCacheReportMemRatio() + config.GetCacheReportMemRatio() +
config.GetCacheStatusMemRatio() + config.GetCacheStatusMemRatio() +
config.GetCacheStatusFaveMemRatio() + config.GetCacheStatusFaveMemRatio() +
config.GetCacheStatusFaveIDsMemRatio() +
config.GetCacheTagMemRatio() + config.GetCacheTagMemRatio() +
config.GetCacheTombstoneMemRatio() + config.GetCacheTombstoneMemRatio() +
config.GetCacheUserMemRatio() + config.GetCacheUserMemRatio() +

View File

@ -91,6 +91,7 @@ func (c *Cache[T]) SetEvictionCallback(hook func(T)) {
if res.Error != nil { if res.Error != nil {
// Skip value hooks // Skip value hooks
putResult(res)
return return
} }
@ -119,6 +120,7 @@ func (c *Cache[T]) SetInvalidateCallback(hook func(T)) {
if res.Error != nil { if res.Error != nil {
// Skip value hooks // Skip value hooks
putResult(res)
return 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. // 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) { func (c *Cache[T]) Load(lookup string, load func() (T, error), keyParts ...any) (T, error) {
var ( var zero T
zero T var res *result
res *result
)
// Get lookup key info by name. // Get lookup key info by name.
keyInfo := c.lookups.get(lookup) 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 // Acquire cache lock
c.cache.Lock() c.cache.Lock()
// Look for primary cache key // Look for primary key for cache key (only accept len=1)
pkeys := keyInfo.pkeys[ckey] if pkeys := keyInfo.pkeys[ckey]; len(pkeys) == 1 {
if len(pkeys) > 0 {
// Fetch the result for primary key // Fetch the result for primary key
entry, ok := c.cache.Cache.Get(pkeys[0]) entry, ok := c.cache.Cache.Get(pkeys[0])
if ok { if ok {
// Since the invalidation / eviction hooks acquire a mutex // Since the invalidation / eviction hooks acquire a mutex
// lock separately, and only at this point are the pkeys // 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 // Acquire cache lock
c.cache.Lock() c.cache.Lock()
// Look for primary key for cache key // Look for primary key for cache key (only accept len=1)
pkeys := keyInfo.pkeys[ckey] if pkeys := keyInfo.pkeys[ckey]; len(pkeys) == 1 {
if len(pkeys) > 0 {
// Fetch the result for primary key // Fetch the result for primary key
entry, ok := c.cache.Cache.Get(pkeys[0]) entry, ok := c.cache.Cache.Get(pkeys[0])
if ok { if ok {
// Since the invalidation / eviction hooks acquire a mutex // Since the invalidation / eviction hooks acquire a mutex
// lock separately, and only at this point are the pkeys // 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 { if key.info.unique && len(pkeys) > 0 {
for _, conflict := range pkeys { for _, conflict := range pkeys {
// Get the overlapping result with this key. // Get the overlapping result with this key.
entry, _ := c.cache.Cache.Get(conflict) entry, ok := c.cache.Cache.Get(conflict)
confRes := entry.Value.(*result)
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 // From conflicting entry, drop this key, this
// will prevent eviction cleanup key confusion. // will prevent eviction cleanup key confusion.
confRes := entry.Value.(*result)
confRes.Keys.drop(key.info.name) confRes.Keys.drop(key.info.name)
if len(res.Keys) == 0 { if len(res.Keys) == 0 {
// We just over-wrote the only lookup key for // We just over-wrote the only lookup key for
// this value, so we drop its primary key too. // 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. // Add finished result to evict queue.
toEvict = append(toEvict, confRes) toEvict = append(toEvict, confRes)

View File

@ -17,6 +17,7 @@ func getResult() *result {
// putResult replaces a result in the pool. // putResult replaces a result in the pool.
func putResult(r *result) { func putResult(r *result) {
r.PKey = 0
r.Keys = nil r.Keys = nil
r.Value = nil r.Value = nil
r.Error = nil r.Error = nil

2
vendor/modules.txt vendored
View File

@ -13,7 +13,7 @@ codeberg.org/gruf/go-bytesize
# codeberg.org/gruf/go-byteutil v1.1.2 # codeberg.org/gruf/go-byteutil v1.1.2
## explicit; go 1.16 ## explicit; go 1.16
codeberg.org/gruf/go-byteutil 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 ## explicit; go 1.19
codeberg.org/gruf/go-cache/v3 codeberg.org/gruf/go-cache/v3
codeberg.org/gruf/go-cache/v3/result codeberg.org/gruf/go-cache/v3/result