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

Implement a fix for #66, excessive memory use in siphon. #67

Open
wants to merge 1 commit 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
19 changes: 13 additions & 6 deletions diskv.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,22 @@ func (s *siphon) Read(p []byte) (int, error) {
n, err := s.f.Read(p)

if err == nil {
return s.buf.Write(p[0:n]) // Write must succeed for Read to succeed
}

if err == io.EOF {
s.d.cacheWithoutLock(s.key, s.buf.Bytes()) // cache may fail
// Only write into the buffer if the buffer is not yet over the size of the cache.
// This logic guarantees that we'll write into the buffer until one of two things happens:
// 1. We write the entire contents of the source into the buffer.
// 2. We've read enough into the buffer to exceed the max cache size.
// If we read more than the max cache size into the buffer, our later attempt
// to update the cache will be rejected (because the value exceeds the max cache size);
// it does *not* cache a partial value.
if uint64(s.buf.Len()) < s.d.CacheSizeMax {
return s.buf.Write(p[0:n]) // Write must succeed for Read to succeed
}
} else if err == io.EOF {
// The cache will reject this if we've exceeded the maximum cache size
s.d.cacheWithoutLock(s.key, s.buf.Bytes())
if closeErr := s.f.Close(); closeErr != nil {
return n, closeErr // close must succeed for Read to succeed
}
return n, err
}

return n, err
Expand Down