Reduce critical section for watchcache.lock

This commit is contained in:
Wojciech Tyczyński 2024-05-21 13:58:35 +02:00
parent 834658cb26
commit 202b4ffdf0

View File

@ -312,14 +312,11 @@ func (w *watchCache) processEvent(event watch.Event, resourceVersion uint64, upd
RecordTime: w.clock.Now(),
}
if err := func() error {
// TODO: We should consider moving this lock below after the watchCacheEvent
// is created. In such situation, the only problematic scenario is Replace()
// happening after getting object from store and before acquiring a lock.
// Maybe introduce another lock for this purpose.
w.Lock()
defer w.Unlock()
// We can call w.store.Get() outside of a critical section,
// because the w.store itself is thread-safe and the only
// place where w.store is modified is below (via updateFunc)
// and these calls are serialized because reflector is processing
// events one-by-one.
previous, exists, err := w.store.Get(elem)
if err != nil {
return err
@ -331,6 +328,10 @@ func (w *watchCache) processEvent(event watch.Event, resourceVersion uint64, upd
wcEvent.PrevObjFields = previousElem.Fields
}
if err := func() error {
w.Lock()
defer w.Unlock()
w.updateCache(wcEvent)
w.resourceVersion = resourceVersion
defer w.cond.Broadcast()