Description
Is your feature request related to a problem? Please describe.
I'm using persistent_term
to store some atomic
counters, which are read and updated by multiple processes concurrently. I'm also creating atomic counters for keys that do not already have one concurrently. Once created, the atomic reference itself (i.e. the term that is persisted) is never updated.
However due to the "overwrite if it already exists" nature of persistent_term:put
, I cannot persist these terms concurrently as I cannot atomically check if the term doesn't exist and then conditionally put the term.
Describe the solution you'd like
I propose that a persistent_term:put_new/2
function is added that puts a term only if doesn't already exist, returning an error or an atom otherwise.
Describe alternatives you've considered
Presently I sequentialize all writes to the persistent_term
from these process with a gen_server
, which has a noticeable performance cost.
Another alternative for my problem is to use an agent
instead. This has significant performance costs and involves not using persistent_term
all together, so I wouldn't call it a real alternative.
Additional context
This already exists with Elixir's Map
as Map.put_new
, though there they just return the original map and do not return any error on a pre-existing key.