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:

https://github.com/general-CbIC/poolex/blob/develop/lib/poolex/private/monitoring.ex#L9

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.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
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
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement