aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/cache/object_lru.go
diff options
context:
space:
mode:
authorkuba-- <kuba@sourced.tech>2018-09-17 22:20:50 +0200
committerkuba-- <kuba@sourced.tech>2018-09-17 22:20:50 +0200
commit4896974b4daf86f53d782c868d408f830f84c294 (patch)
treec0668f6aeb4f95c5d75bef0e2aa469b563d10810 /plumbing/cache/object_lru.go
parent2fb32d2a8601213b6db109d3e9028c6b64af1874 (diff)
downloadgo-git-4896974b4daf86f53d782c868d408f830f84c294.tar.gz
Fix potential LRU cache size issue.
Signed-off-by: kuba-- <kuba@sourced.tech>
Diffstat (limited to 'plumbing/cache/object_lru.go')
-rw-r--r--plumbing/cache/object_lru.go24
1 files changed, 13 insertions, 11 deletions
diff --git a/plumbing/cache/object_lru.go b/plumbing/cache/object_lru.go
index 0494539..31c0202 100644
--- a/plumbing/cache/object_lru.go
+++ b/plumbing/cache/object_lru.go
@@ -42,20 +42,26 @@ func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
c.ll = list.New()
}
+ objSize := FileSize(obj.Size())
key := obj.Hash()
if ee, ok := c.cache[key]; ok {
+ oldObj := ee.Value.(plumbing.EncodedObject)
+ // in this case objSize is a delta: new size - old size
+ objSize -= FileSize(oldObj.Size())
+
c.ll.MoveToFront(ee)
ee.Value = obj
- return
- }
-
- objSize := FileSize(obj.Size())
+ } else {
+ if objSize > c.MaxSize {
+ return
+ }
- if objSize > c.MaxSize {
- return
+ ee := c.ll.PushFront(obj)
+ c.cache[key] = ee
}
- for c.actualSize+objSize > c.MaxSize {
+ c.actualSize += objSize
+ for c.actualSize > c.MaxSize {
last := c.ll.Back()
lastObj := last.Value.(plumbing.EncodedObject)
lastSize := FileSize(lastObj.Size())
@@ -64,10 +70,6 @@ func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
delete(c.cache, lastObj.Hash())
c.actualSize -= lastSize
}
-
- ee := c.ll.PushFront(obj)
- c.cache[key] = ee
- c.actualSize += objSize
}
// Get returns an object by its hash. It marks the object as used. If the object