mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-12 13:31:52 +00:00
Merge pull request #32244 from tksm/fix-cache-race
Automatic merge from submit-queue LRUExpireCache#Get requires write lock **What this PR does / why we need it**: [LRUExpireCache#Get](dbfad789e3/pkg/util/cache/lruexpirecache.go (L48)
) requires write lock since [groupcache/lru#Get](a6b377e340/lru/lru.go (L74)
) needs to manipulate its list to track recently used item. Currently it uses read lock so it may introduce race condition. - [test code which introduces race condition with current LRUExpireCache#Get](https://gist.github.com/tksm/17c7a610ed0574c165e6f6edeca351b7#file-lru_race_test-go) **Which issue this PR fixes** #31081
This commit is contained in:
commit
804de8a149
6
pkg/util/cache/lruexpirecache.go
vendored
6
pkg/util/cache/lruexpirecache.go
vendored
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
type LRUExpireCache struct {
|
type LRUExpireCache struct {
|
||||||
cache *lru.Cache
|
cache *lru.Cache
|
||||||
lock sync.RWMutex
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLRUExpireCache(maxSize int) *LRUExpireCache {
|
func NewLRUExpireCache(maxSize int) *LRUExpireCache {
|
||||||
@ -46,8 +46,8 @@ func (c *LRUExpireCache) Add(key lru.Key, value interface{}, ttl time.Duration)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *LRUExpireCache) Get(key lru.Key) (interface{}, bool) {
|
func (c *LRUExpireCache) Get(key lru.Key) (interface{}, bool) {
|
||||||
c.lock.RLock()
|
c.lock.Lock()
|
||||||
defer c.lock.RUnlock()
|
defer c.lock.Unlock()
|
||||||
e, ok := c.cache.Get(key)
|
e, ok := c.cache.Get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, false
|
return nil, false
|
||||||
|
Loading…
Reference in New Issue
Block a user