knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/configmap/informer/synced_callback.go (about) 1 /* 2 Copyright 2021 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package informer 18 19 import ( 20 "context" 21 "sync" 22 23 "k8s.io/apimachinery/pkg/util/sets" 24 ) 25 26 // namedWaitGroup is used to increment and decrement a WaitGroup by name 27 type namedWaitGroup struct { 28 waitGroup sync.WaitGroup 29 keys sets.Set[string] 30 mu sync.Mutex 31 } 32 33 // newNamedWaitGroup returns an instantiated namedWaitGroup. 34 func newNamedWaitGroup() *namedWaitGroup { 35 return &namedWaitGroup{ 36 keys: sets.New[string](), 37 } 38 } 39 40 // Add will add the key to the list of keys being tracked and increment the wait group. 41 // If the key has already been added, the wait group will not be incremented again. 42 func (n *namedWaitGroup) Add(key string) { 43 n.mu.Lock() 44 defer n.mu.Unlock() 45 46 if !n.keys.Has(key) { 47 n.keys.Insert(key) 48 n.waitGroup.Add(1) 49 } 50 } 51 52 // Done will decrement the counter if the key is present in the tracked keys. If it is not present 53 // it will be ignored. 54 func (n *namedWaitGroup) Done(key string) { 55 n.mu.Lock() 56 defer n.mu.Unlock() 57 58 if n.keys.Has(key) { 59 n.keys.Delete(key) 60 n.waitGroup.Done() 61 } 62 } 63 64 // Wait will wait for the underlying waitGroup to complete. 65 func (n *namedWaitGroup) Wait() { 66 n.waitGroup.Wait() 67 } 68 69 // syncedCallback can be used to wait for a callback to be called at least once for a list of keys. 70 type syncedCallback struct { 71 // namedWaitGroup will block until the callback has been called for all tracked entities 72 namedWaitGroup *namedWaitGroup 73 74 // callback is the callback that is intended to be called at least once for each key 75 // being tracked via WaitGroup 76 callback func(obj interface{}) 77 } 78 79 // newSyncedCallback will return a syncedCallback that will track the provided keys. 80 func newSyncedCallback(keys []string, callback func(obj interface{})) *syncedCallback { 81 s := &syncedCallback{ 82 callback: callback, 83 namedWaitGroup: newNamedWaitGroup(), 84 } 85 for _, key := range keys { 86 s.namedWaitGroup.Add(key) 87 } 88 return s 89 } 90 91 // Event is intended to be a wrapper for the actual event handler; this wrapper will signal via 92 // the wait group that the event handler has been called at least once for the key. 93 func (s *syncedCallback) Call(obj interface{}, key string) { 94 s.callback(obj) 95 s.namedWaitGroup.Done(key) 96 } 97 98 // WaitForAllKeys will block until s.Call has been called for all the keys we are tracking or the stop signal is 99 // received. 100 func (s *syncedCallback) WaitForAllKeys(stopCh <-chan struct{}) error { 101 c := make(chan struct{}) 102 go func() { 103 defer close(c) 104 s.namedWaitGroup.Wait() 105 }() 106 select { 107 case <-c: 108 return nil 109 case <-stopCh: 110 return context.DeadlineExceeded 111 } 112 }