spacebat

spacebat

Dynamic sets of atoms used as keys

While writing an Elixir application I’ve found twice now - using Quantum to create schedules and using Poolex to create worker pools - that these entities are keyed by atoms.

The problem is that the set of schedules or pools should correspond to database records in the application I’m writing, so it would be most natural to key them by the UUID of those records.

Knowing that the application may be long running, atoms are not garbage collected, and the set of ids will change over time, I’ve written a module to map from arbitrary terms (strings in this case) to dynamically generated atoms - the idea being that when an element is deleted from the map, the corresponding atom goes into a free set for reuse, so that the number of atoms allocated over time is no larger than the maximum number of dynamic elements in play.

It seems to me this must have been solved before but I haven’t found the library to reach for. Also, is there a good reason for these hex packages to key their elements by atom?

Most Liked

al2o3cr

al2o3cr

Adding onto the above: I’ve tried this out locally, and it almost works.

One sticking point is in Poolex.Private.Monitoring on line 9:

This crashes if pool_id is a tuple because the tuple doesn’t work with String.Chars.

“Fixing” it by using :"#{inspect(pool_id)}_references" seems to work, but ends up right back at the making-atoms-dynamically-at-runtime mess we were trying to avoid. :thinking:

Going to poke at this a bit more when I’ve got time this week.

general-CbIC

general-CbIC

Full support for GenServer.name() was implemented in version 1.0.0-rc.0. I’m testing the version in our applications, and so far everything looks good. If you check it yourself, please share the results.

al2o3cr

al2o3cr

Skimming the source of Poolex, it seems like the pool_id value is always used as the first argument to GenServer.call and GenServer.cast.

Dialyzer will complain, but you could pass anything from GenServer.name() there - for instance, a :via tuple against a Registry that tracks pools by UUID.

general-CbIC

general-CbIC

This is an excellent point about the Registry and :via tuples. It can be the best solution for supporting this kind of pool ID.

Where Next?

Popular in Questions Top

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement