diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2018-09-18 12:18:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-18 12:18:00 +0200 |
commit | f69f5304328165153e452382f44aa2b41df36d0f (patch) | |
tree | 58a7fb3508e0f7dd637a1e4f62984bfcaa7cb84b /plumbing/cache/buffer_lru.go | |
parent | 2fb32d2a8601213b6db109d3e9028c6b64af1874 (diff) | |
parent | edfc16e3ea6b0ce2533bacb5f370d042042b4784 (diff) | |
download | go-git-f69f5304328165153e452382f44aa2b41df36d0f.tar.gz |
Merge pull request #958 from kuba--/fix-cachesize
Fix potential LRU cache size issue.
Diffstat (limited to 'plumbing/cache/buffer_lru.go')
-rw-r--r-- | plumbing/cache/buffer_lru.go | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/plumbing/cache/buffer_lru.go b/plumbing/cache/buffer_lru.go index f2c0f90..acaf195 100644 --- a/plumbing/cache/buffer_lru.go +++ b/plumbing/cache/buffer_lru.go @@ -45,19 +45,23 @@ func (c *BufferLRU) Put(key int64, slice []byte) { c.ll = list.New() } + bufSize := FileSize(len(slice)) if ee, ok := c.cache[key]; ok { + oldBuf := ee.Value.(buffer) + // in this case bufSize is a delta: new size - old size + bufSize -= FileSize(len(oldBuf.Slice)) c.ll.MoveToFront(ee) ee.Value = buffer{key, slice} - return + } else { + if bufSize > c.MaxSize { + return + } + ee := c.ll.PushFront(buffer{key, slice}) + c.cache[key] = ee } - objSize := FileSize(len(slice)) - - if objSize > c.MaxSize { - return - } - - for c.actualSize+objSize > c.MaxSize { + c.actualSize += bufSize + for c.actualSize > c.MaxSize { last := c.ll.Back() lastObj := last.Value.(buffer) lastSize := FileSize(len(lastObj.Slice)) @@ -66,10 +70,6 @@ func (c *BufferLRU) Put(key int64, slice []byte) { delete(c.cache, lastObj.Key) c.actualSize -= lastSize } - - ee := c.ll.PushFront(buffer{key, slice}) - c.cache[key] = ee - c.actualSize += objSize } // Get returns a buffer by its key. It marks the buffer as used. If the buffer |