mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-21 12:48:30 +00:00
Shrink mutation detection critical section
Kubernetes-commit: 12abf03f6b4a60fa61773acd21dfb440ff10f699
This commit is contained in:
parent
ce0298a60b
commit
b0779d525a
25
tools/cache/mutation_detector.go
vendored
25
tools/cache/mutation_detector.go
vendored
@ -68,7 +68,13 @@ type defaultCacheMutationDetector struct {
|
|||||||
name string
|
name string
|
||||||
period time.Duration
|
period time.Duration
|
||||||
|
|
||||||
lock sync.Mutex
|
// compareLock ensures only a single call to CompareObjects runs at a time
|
||||||
|
compareObjectsLock sync.Mutex
|
||||||
|
|
||||||
|
// addLock guards addedObjs between AddObject and CompareObjects
|
||||||
|
addedObjsLock sync.Mutex
|
||||||
|
addedObjs []cacheObj
|
||||||
|
|
||||||
cachedObjs []cacheObj
|
cachedObjs []cacheObj
|
||||||
|
|
||||||
retainDuration time.Duration
|
retainDuration time.Duration
|
||||||
@ -118,15 +124,22 @@ func (d *defaultCacheMutationDetector) AddObject(obj interface{}) {
|
|||||||
if obj, ok := obj.(runtime.Object); ok {
|
if obj, ok := obj.(runtime.Object); ok {
|
||||||
copiedObj := obj.DeepCopyObject()
|
copiedObj := obj.DeepCopyObject()
|
||||||
|
|
||||||
d.lock.Lock()
|
d.addedObjsLock.Lock()
|
||||||
defer d.lock.Unlock()
|
defer d.addedObjsLock.Unlock()
|
||||||
d.cachedObjs = append(d.cachedObjs, cacheObj{cached: obj, copied: copiedObj})
|
d.addedObjs = append(d.addedObjs, cacheObj{cached: obj, copied: copiedObj})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *defaultCacheMutationDetector) CompareObjects() {
|
func (d *defaultCacheMutationDetector) CompareObjects() {
|
||||||
d.lock.Lock()
|
d.compareObjectsLock.Lock()
|
||||||
defer d.lock.Unlock()
|
defer d.compareObjectsLock.Unlock()
|
||||||
|
|
||||||
|
// move addedObjs into cachedObjs under lock
|
||||||
|
// this keeps the critical section small to avoid blocking AddObject while we compare cachedObjs
|
||||||
|
d.addedObjsLock.Lock()
|
||||||
|
d.cachedObjs = append(d.cachedObjs, d.addedObjs...)
|
||||||
|
d.addedObjs = nil
|
||||||
|
d.addedObjsLock.Unlock()
|
||||||
|
|
||||||
altered := false
|
altered := false
|
||||||
for i, obj := range d.cachedObjs {
|
for i, obj := range d.cachedObjs {
|
||||||
|
Loading…
Reference in New Issue
Block a user