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