diff options
author | kuba-- <kuba@sourced.tech> | 2018-09-17 22:20:50 +0200 |
---|---|---|
committer | kuba-- <kuba@sourced.tech> | 2018-09-17 22:20:50 +0200 |
commit | 4896974b4daf86f53d782c868d408f830f84c294 (patch) | |
tree | c0668f6aeb4f95c5d75bef0e2aa469b563d10810 /plumbing/cache/buffer_lru.go | |
parent | 2fb32d2a8601213b6db109d3e9028c6b64af1874 (diff) | |
download | go-git-4896974b4daf86f53d782c868d408f830f84c294.tar.gz |
Fix potential LRU cache size issue.
Signed-off-by: kuba-- <kuba@sourced.tech>
Diffstat (limited to 'plumbing/cache/buffer_lru.go')
-rw-r--r-- | plumbing/cache/buffer_lru.go | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/plumbing/cache/buffer_lru.go b/plumbing/cache/buffer_lru.go index f2c0f90..e86ccb2 100644 --- a/plumbing/cache/buffer_lru.go +++ b/plumbing/cache/buffer_lru.go @@ -45,19 +45,25 @@ 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 + } - objSize := FileSize(len(slice)) - - if objSize > c.MaxSize { - return + ee := c.ll.PushFront(buffer{key, slice}) + c.cache[key] = ee } - 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 +72,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 |