File tree Expand file tree Collapse file tree 1 file changed +19
-6
lines changed
staging/src/k8s.io/client-go/tools/cache Expand file tree Collapse file tree 1 file changed +19
-6
lines changed Original file line number Diff line number Diff line change @@ -68,7 +68,13 @@ type defaultCacheMutationDetector struct {
68
68
name string
69
69
period time.Duration
70
70
71
- lock sync.Mutex
71
+ // compareLock ensures only a single call to CompareObjects runs at a time
72
+ compareObjectsLock sync.Mutex
73
+
74
+ // addLock guards addedObjs between AddObject and CompareObjects
75
+ addedObjsLock sync.Mutex
76
+ addedObjs []cacheObj
77
+
72
78
cachedObjs []cacheObj
73
79
74
80
retainDuration time.Duration
@@ -118,15 +124,22 @@ func (d *defaultCacheMutationDetector) AddObject(obj interface{}) {
118
124
if obj , ok := obj .(runtime.Object ); ok {
119
125
copiedObj := obj .DeepCopyObject ()
120
126
121
- d .lock .Lock ()
122
- defer d .lock .Unlock ()
123
- d .cachedObjs = append (d .cachedObjs , cacheObj {cached : obj , copied : copiedObj })
127
+ d .addedObjsLock .Lock ()
128
+ defer d .addedObjsLock .Unlock ()
129
+ d .addedObjs = append (d .addedObjs , cacheObj {cached : obj , copied : copiedObj })
124
130
}
125
131
}
126
132
127
133
func (d * defaultCacheMutationDetector ) CompareObjects () {
128
- d .lock .Lock ()
129
- defer d .lock .Unlock ()
134
+ d .compareObjectsLock .Lock ()
135
+ defer d .compareObjectsLock .Unlock ()
136
+
137
+ // move addedObjs into cachedObjs under lock
138
+ // this keeps the critical section small to avoid blocking AddObject while we compare cachedObjs
139
+ d .addedObjsLock .Lock ()
140
+ d .cachedObjs = append (d .cachedObjs , d .addedObjs ... )
141
+ d .addedObjs = nil
142
+ d .addedObjsLock .Unlock ()
130
143
131
144
altered := false
132
145
for i , obj := range d .cachedObjs {
You can’t perform that action at this time.
0 commit comments