MCPcopy
hub / github.com/kubernetes/client-go / AddIfNotPresent

Method AddIfNotPresent

tools/cache/heap.go:192–205  ·  view source on GitHub ↗

AddIfNotPresent inserts an item, and puts it in the queue. If an item with the key is present in the map, no changes is made to the item. This is useful in a single producer/consumer scenario so that the consumer can safely retry items without contending with the producer and potentially enqueueing

(obj interface{})

Source from the content-addressed store, hash-verified

190// safely retry items without contending with the producer and potentially enqueueing
191// stale items.
192func (h *Heap) AddIfNotPresent(obj interface{}) error {
193 id, err := h.data.keyFunc(obj)
194 if err != nil {
195 return KeyError{obj, err}
196 }
197 h.lock.Lock()
198 defer h.lock.Unlock()
199 if h.closed {
200 return fmt.Errorf(closedMsg)
201 }
202 h.addIfNotPresentLocked(id, obj)
203 h.cond.Broadcast()
204 return nil
205}
206
207// addIfNotPresentLocked assumes the lock is already held and adds the provided
208// item to the queue if it does not already exist.

Callers 2

TestHeap_AddIfNotPresentFunction · 0.95
TestHeapAddAfterCloseFunction · 0.95

Calls 2

addIfNotPresentLockedMethod · 0.95
ErrorfMethod · 0.65

Tested by 2

TestHeap_AddIfNotPresentFunction · 0.76
TestHeapAddAfterCloseFunction · 0.76