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{})
| 190 | // safely retry items without contending with the producer and potentially enqueueing |
| 191 | // stale items. |
| 192 | func (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. |