Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSami Hiltunen <shiltunen@gitlab.com>2023-10-02 12:45:16 +0300
committerSami Hiltunen <shiltunen@gitlab.com>2023-10-02 12:45:16 +0300
commit86ca544ff07406eda16b1aab734c1f3dbff9d31d (patch)
tree74672b013b1f3a112a0f1e671de37cbe67bb5062
parentd481287fe6bd9393eb7737c81173be009c1670c0 (diff)
parent7e8c103110fdbfa8ffa907fbdc3b36872efd93d8 (diff)
Merge branch 'renovate/github.com-hashicorp-golang-lru-v2-2.x' into 'master'
go: Update module github.com/hashicorp/golang-lru/v2 to v2.0.7 See merge request https://gitlab.com/gitlab-org/gitaly/-/merge_requests/6431 Merged-by: Sami Hiltunen <shiltunen@gitlab.com> Approved-by: James Fargher <jfargher@gitlab.com> Approved-by: Sami Hiltunen <shiltunen@gitlab.com> Co-authored-by: GitLab Renovate Bot <gitlab-bot@gitlab.com>
-rw-r--r--NOTICE121
-rw-r--r--go.mod2
-rw-r--r--go.sum4
3 files changed, 119 insertions, 8 deletions
diff --git a/NOTICE b/NOTICE
index a30395535..97b9b36d6 100644
--- a/NOTICE
+++ b/NOTICE
@@ -11669,8 +11669,10 @@ const (
// head. The ARCCache is similar, but does not require setting any
// parameters.
type TwoQueueCache[K comparable, V any] struct {
- size int
- recentSize int
+ size int
+ recentSize int
+ recentRatio float64
+ ghostRatio float64
recent simplelru.LRUCache[K, V]
frequent simplelru.LRUCache[K, V]
@@ -11719,6 +11721,8 @@ func New2QParams[K comparable, V any](size int, recentRatio, ghostRatio float64)
c := &TwoQueueCache[K, V]{
size: size,
recentSize: recentSize,
+ recentRatio: recentRatio,
+ ghostRatio: ghostRatio,
recent: recent,
frequent: frequent,
recentEvict: recentEvict,
@@ -11810,6 +11814,34 @@ func (c *TwoQueueCache[K, V]) Len() int {
return c.recent.Len() + c.frequent.Len()
}
+// Resize changes the cache size.
+func (c *TwoQueueCache[K, V]) Resize(size int) (evicted int) {
+ c.lock.Lock()
+ defer c.lock.Unlock()
+
+ // Recalculate the sub-sizes
+ recentSize := int(float64(size) * c.recentRatio)
+ evictSize := int(float64(size) * c.ghostRatio)
+ c.size = size
+ c.recentSize = recentSize
+
+ // ensureSpace
+ diff := c.recent.Len() + c.frequent.Len() - size
+ if diff < 0 {
+ diff = 0
+ }
+ for i := 0; i < diff; i++ {
+ c.ensureSpace(true)
+ }
+
+ // Reallocate the LRUs
+ c.recent.Resize(size)
+ c.frequent.Resize(size)
+ c.recentEvict.Resize(evictSize)
+
+ return diff
+}
+
// Keys returns a slice of the keys in the cache.
// The frequently used keys are first in the returned slice.
func (c *TwoQueueCache[K, V]) Keys() []K {
@@ -12095,6 +12127,75 @@ func Test2Q_Add_RecentEvict(t *testing.T) {
}
}
+func Test2Q_Resize(t *testing.T) {
+ l, err := New2Q[int, int](100)
+ if err != nil {
+ t.Fatalf("err: %v", err)
+ }
+
+ // Touch all the entries, should be in t1
+ for i := 0; i < 100; i++ {
+ l.Add(i, i)
+ }
+
+ evicted := l.Resize(50)
+ if evicted != 50 {
+ t.Fatalf("bad: %d", evicted)
+ }
+
+ if n := l.recent.Len(); n != 50 {
+ t.Fatalf("bad: %d", n)
+ }
+ if n := l.frequent.Len(); n != 0 {
+ t.Fatalf("bad: %d", n)
+ }
+
+ l, err = New2Q[int, int](100)
+ if err != nil {
+ t.Fatalf("err: %v", err)
+ }
+ for i := 0; i < 100; i++ {
+ l.Add(i, i)
+ }
+
+ for i := 0; i < 50; i++ {
+ l.Add(i, i)
+ }
+
+ evicted = l.Resize(50)
+ if evicted != 50 {
+ t.Fatalf("bad: %d", evicted)
+ }
+
+ if n := l.recent.Len(); n != 12 {
+ t.Fatalf("bad: %d", n)
+ }
+ if n := l.frequent.Len(); n != 38 {
+ t.Fatalf("bad: %d", n)
+ }
+
+ l, err = New2Q[int, int](100)
+ if err != nil {
+ t.Fatalf("err: %v", err)
+ }
+ for i := 0; i < 100; i++ {
+ l.Add(i, i)
+ l.Add(i, i)
+ }
+
+ evicted = l.Resize(50)
+ if evicted != 50 {
+ t.Fatalf("bad: %d", evicted)
+ }
+
+ if n := l.recent.Len(); n != 0 {
+ t.Fatalf("bad: %d", n)
+ }
+ if n := l.frequent.Len(); n != 50 {
+ t.Fatalf("bad: %d", n)
+ }
+}
+
func Test2Q(t *testing.T) {
l, err := New2Q[int, int](128)
if err != nil {
@@ -12815,7 +12916,7 @@ func (c *LRU[K, V]) Get(key K) (value V, ok bool) {
if ent, ok = c.items[key]; ok {
// Expired item check
if time.Now().After(ent.ExpiresAt) {
- return
+ return value, false
}
c.evictList.MoveToFront(ent)
return ent.Value, true
@@ -12841,7 +12942,7 @@ func (c *LRU[K, V]) Peek(key K) (value V, ok bool) {
if ent, ok = c.items[key]; ok {
// Expired item check
if time.Now().After(ent.ExpiresAt) {
- return
+ return value, false
}
return ent.Value, true
}
@@ -13393,7 +13494,17 @@ func TestLoadingExpired(t *testing.T) {
t.Fatalf("should be true")
}
- time.Sleep(time.Millisecond * 100) // wait for entry to expire
+ for {
+ result, ok := lc.Get("key1")
+ if ok && result == "" {
+ t.Fatalf("ok should return a result")
+ }
+ if !ok {
+ break
+ }
+ }
+
+ time.Sleep(time.Millisecond * 100) // wait for expiration reaper
if lc.Len() != 0 {
t.Fatalf("length differs from expected")
}
diff --git a/go.mod b/go.mod
index 67335f686..23a1f7eb9 100644
--- a/go.mod
+++ b/go.mod
@@ -20,7 +20,7 @@ require (
github.com/google/uuid v1.3.1
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
- github.com/hashicorp/golang-lru/v2 v2.0.6
+ github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/hashicorp/yamux v0.1.2-0.20220728231024-8f49b6f63f18
github.com/jackc/pgx/v5 v5.4.3
github.com/kelseyhightower/envconfig v1.4.0
diff --git a/go.sum b/go.sum
index 3916dc6a7..82bad8c82 100644
--- a/go.sum
+++ b/go.sum
@@ -391,8 +391,8 @@ github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/C
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
-github.com/hashicorp/golang-lru/v2 v2.0.6 h1:3xi/Cafd1NaoEnS/yDssIiuVeDVywU0QdFGl3aQaQHM=
-github.com/hashicorp/golang-lru/v2 v2.0.6/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
+github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
+github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/yamux v0.1.2-0.20220728231024-8f49b6f63f18 h1:IVujPV6DRIu1fYF4zUHrfhkngJzmYjelXa+iSUiFZSI=
github.com/hashicorp/yamux v0.1.2-0.20220728231024-8f49b6f63f18/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/hhatto/gorst v0.0.0-20181029133204-ca9f730cac5b h1:Jdu2tbAxkRouSILp2EbposIb8h4gO+2QuZEn3d9sKAc=