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

Make the strict memory limit optional #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 20 additions & 9 deletions diskv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
NoPanicOnMemoryLimit bool
// If TempDir is set, it will enable filesystem atomic writes by
// writing temporary files to that location before being moved
// to BasePath.
Expand Down Expand Up @@ -640,7 +641,12 @@ func (d *Diskv) cacheWithLock(key string, val []byte) error {

// be very strict about memory guarantees
if (d.cacheSize + valueSize) > d.CacheSizeMax {
panic(fmt.Sprintf("failed to make room for value (%d/%d)", 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
Expand Down Expand Up @@ -713,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
Expand Down