From 91c8c31e1aa77404b5dbd32808607153cd9d02e8 Mon Sep 17 00:00:00 2001 From: Eric Lindvall Date: Wed, 20 Jan 2021 11:36:17 -0800 Subject: [PATCH 1/2] Make the strict memory limit optional --- diskv.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/diskv.go b/diskv.go index 9f07b85..c4f27a0 100644 --- a/diskv.go +++ b/diskv.go @@ -69,13 +69,14 @@ type InverseTransformFunction func(pathKey *PathKey) string // Options define a set of properties that dictate Diskv behavior. // All values are optional. type Options struct { - BasePath string - Transform TransformFunction - AdvancedTransform AdvancedTransformFunction - InverseTransform InverseTransformFunction - CacheSizeMax uint64 // bytes - PathPerm os.FileMode - FilePerm os.FileMode + BasePath string + Transform TransformFunction + AdvancedTransform AdvancedTransformFunction + InverseTransform InverseTransformFunction + CacheSizeMax uint64 // bytes + PathPerm os.FileMode + FilePerm os.FileMode + NoStrictMemoryLimit bool // If TempDir is set, it will enable filesystem atomic writes by // writing temporary files to that location before being moved // to BasePath. @@ -639,7 +640,7 @@ func (d *Diskv) cacheWithLock(key string, val []byte) error { } // be very strict about memory guarantees - if (d.cacheSize + valueSize) > d.CacheSizeMax { + if (d.cacheSize+valueSize) > d.CacheSizeMax && !d.NoStrictMemoryLimit { panic(fmt.Sprintf("failed to make room for value (%d/%d)", valueSize, d.CacheSizeMax)) } From 733a2ded1211e9d38a7ef984639b872140a87a81 Mon Sep 17 00:00:00 2001 From: Eric Lindvall Date: Wed, 27 Jan 2021 11:26:43 -0800 Subject: [PATCH 2/2] Find all the places that are panicing --- diskv.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/diskv.go b/diskv.go index c4f27a0..010a8ef 100644 --- a/diskv.go +++ b/diskv.go @@ -69,14 +69,14 @@ type InverseTransformFunction func(pathKey *PathKey) string // Options define a set of properties that dictate Diskv behavior. // All values are optional. type Options struct { - BasePath string - Transform TransformFunction - AdvancedTransform AdvancedTransformFunction - InverseTransform InverseTransformFunction - CacheSizeMax uint64 // bytes - PathPerm os.FileMode - FilePerm os.FileMode - NoStrictMemoryLimit bool + BasePath string + Transform TransformFunction + AdvancedTransform AdvancedTransformFunction + InverseTransform InverseTransformFunction + CacheSizeMax uint64 // bytes + PathPerm os.FileMode + FilePerm os.FileMode + NoPanicOnMemoryLimit bool // If TempDir is set, it will enable filesystem atomic writes by // writing temporary files to that location before being moved // to BasePath. @@ -640,8 +640,13 @@ func (d *Diskv) cacheWithLock(key string, val []byte) error { } // be very strict about memory guarantees - if (d.cacheSize+valueSize) > d.CacheSizeMax && !d.NoStrictMemoryLimit { - panic(fmt.Sprintf("failed to make room for value (%d/%d)", valueSize, d.CacheSizeMax)) + if (d.cacheSize + valueSize) > d.CacheSizeMax { + err := fmt.Sprintf("failed to make room for value (%d/%d/%d)", valueSize, d.cacheSize, d.CacheSizeMax) + if d.NoPanicOnMemoryLimit { + return fmt.Errorf("%s; not caching", err) + } + + panic(err) } d.cache[key] = val @@ -714,7 +719,12 @@ func (d *Diskv) ensureCacheSpaceWithLock(valueSize uint64) error { } if !safe() { - panic(fmt.Sprintf("%d bytes still won't fit in the cache! (max %d bytes)", valueSize, d.CacheSizeMax)) + err := fmt.Errorf("%d bytes still won't fit in the cache! (cache size %d bytes, max %d bytes)", valueSize, d.cacheSize, d.CacheSizeMax) + if d.NoPanicOnMemoryLimit { + return err + } + + panic(err) } return nil