Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 526d60a

Browse files
committed
fix concurrent cache issues
1 parent 62c6e3d commit 526d60a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

pkg/cache/cache.go

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cache
22

33
import (
44
"fmt"
5+
"sync"
56
"time"
67

78
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -14,15 +15,20 @@ type cacheItem struct {
1415

1516
type ResourceCache struct {
1617
resources map[string]cacheItem
18+
lock *sync.RWMutex
1719
}
1820

1921
func NewCache() *ResourceCache {
2022
return &ResourceCache{
2123
resources: map[string]cacheItem{},
24+
lock: &sync.RWMutex{},
2225
}
2326
}
2427

2528
func (rc *ResourceCache) Get(obj *unstructured.Unstructured) (*unstructured.Unstructured, time.Time) {
29+
rc.lock.RLock()
30+
defer rc.lock.RUnlock()
31+
2632
existing, exists := rc.resources[rc.objectKey(obj)]
2733
if !exists {
2834
return nil, time.Time{}
@@ -32,13 +38,19 @@ func (rc *ResourceCache) Get(obj *unstructured.Unstructured) (*unstructured.Unst
3238
}
3339

3440
func (rc *ResourceCache) Set(obj *unstructured.Unstructured) {
41+
rc.lock.Lock()
42+
defer rc.lock.Unlock()
43+
3544
rc.resources[rc.objectKey(obj)] = cacheItem{
3645
resource: obj.DeepCopy(),
3746
lastSeen: time.Now(),
3847
}
3948
}
4049

4150
func (rc *ResourceCache) Delete(obj *unstructured.Unstructured) {
51+
rc.lock.Lock()
52+
defer rc.lock.Unlock()
53+
4254
delete(rc.resources, rc.objectKey(obj))
4355
}
4456

0 commit comments

Comments
 (0)