aboutsummaryrefslogtreecommitdiff
path: root/weed/filer/reader_cache.go
diff options
context:
space:
mode:
Diffstat (limited to 'weed/filer/reader_cache.go')
-rw-r--r--weed/filer/reader_cache.go17
1 files changed, 13 insertions, 4 deletions
diff --git a/weed/filer/reader_cache.go b/weed/filer/reader_cache.go
index c319f6c78..4f375e764 100644
--- a/weed/filer/reader_cache.go
+++ b/weed/filer/reader_cache.go
@@ -18,7 +18,7 @@ type ReaderCache struct {
}
type SingleChunkCacher struct {
- sync.RWMutex
+ sync.Mutex
cond *sync.Cond
parent *ReaderCache
chunkFileId string
@@ -76,7 +76,9 @@ func (rc *ReaderCache) ReadChunkAt(buffer []byte, fileId string, cipherKey []byt
rc.Lock()
defer rc.Unlock()
if cacher, found := rc.downloaders[fileId]; found {
- return cacher.readChunkAt(buffer, offset)
+ if n, err := cacher.readChunkAt(buffer, offset); n != 0 && err == nil {
+ return n, err
+ }
}
if shouldCache || rc.lookupFileIdFn == nil {
n, err := rc.chunkCache.ReadChunkAt(buffer, fileId, uint64(offset))
@@ -176,6 +178,9 @@ func (s *SingleChunkCacher) startCaching() {
}
func (s *SingleChunkCacher) destroy() {
+ s.Lock()
+ defer s.Unlock()
+
if s.data != nil {
mem.Free(s.data)
s.data = nil
@@ -183,8 +188,8 @@ func (s *SingleChunkCacher) destroy() {
}
func (s *SingleChunkCacher) readChunkAt(buf []byte, offset int64) (int, error) {
- s.RLock()
- defer s.RUnlock()
+ s.Lock()
+ defer s.Unlock()
for s.completedTime.IsZero() {
s.cond.Wait()
@@ -194,6 +199,10 @@ func (s *SingleChunkCacher) readChunkAt(buf []byte, offset int64) (int, error) {
return 0, s.err
}
+ if len(s.data) == 0 {
+ return 0, nil
+ }
+
return copy(buf, s.data[offset:]), nil
}