Delete decrements the usage count for key and removes the value from the underlying map if the usage is 0. It returns true if the usage count reached 0 and the value was deleted. It panics if the usage count drops below 0; always call Delete precisely as many times as LoadOrStore.
(key any)
| 169 | // It panics if the usage count drops below 0; always call |
| 170 | // Delete precisely as many times as LoadOrStore. |
| 171 | func (up *UsagePool) Delete(key any) (deleted bool, err error) { |
| 172 | up.Lock() |
| 173 | upv, ok := up.pool[key] |
| 174 | if !ok { |
| 175 | up.Unlock() |
| 176 | return false, nil |
| 177 | } |
| 178 | refs := upv.refs.Add(-1) |
| 179 | if refs == 0 { |
| 180 | delete(up.pool, key) |
| 181 | up.Unlock() |
| 182 | upv.RLock() |
| 183 | val := upv.value |
| 184 | upv.RUnlock() |
| 185 | if destructor, ok := val.(Destructor); ok { |
| 186 | err = destructor.Destruct() |
| 187 | } |
| 188 | deleted = true |
| 189 | } else { |
| 190 | up.Unlock() |
| 191 | if refs < 0 { |
| 192 | panic(fmt.Sprintf("deleted more than stored: %#v (usage: %d)", |
| 193 | upv.value, upv.refs.Load())) |
| 194 | } |
| 195 | } |
| 196 | return deleted, err |
| 197 | } |
| 198 | |
| 199 | // References returns the number of references (count of usages) to a |
| 200 | // key in the pool, and true if the key exists, or false otherwise. |