Merge pull request #26758 from mqliang/lookupcache-threadsafe

Automatic merge from submit-queue

bugfix:lookupcache's Get method can not be called concurrently

ref https://github.com/kubernetes/kubernetes/issues/26376

@lavalamp @therc @mikedanese
pull/6/head
k8s-merge-robot 2016-06-03 12:46:13 -07:00
commit a00dbea133
1 changed files with 4 additions and 2 deletions

View File

@ -72,8 +72,10 @@ func (c *MatchingCache) Add(labelObj objectWithMeta, selectorObj objectWithMeta)
// we need check in the external request to ensure the cache data is not dirty.
func (c *MatchingCache) GetMatchingObject(labelObj objectWithMeta) (controller interface{}, exists bool) {
key := keyFunc(labelObj)
c.mutex.RLock()
defer c.mutex.RUnlock()
// NOTE: we use Lock() instead of RLock() here because lru's Get() method also modifies state(
// it need update the least recently usage information). So we can not call it concurrently.
c.mutex.Lock()
defer c.mutex.Unlock()
return c.cache.Get(key)
}