Keep a list of connection pids, with urls as keys

I am keeping connections to a list of servers represented by URLs. Each of them has a pid if connected.

Ideally, I’d like to have a simple list only containing the connected ones. It should also be possible to index by by URL to get a specific pid. Should also be easily updatable…

Been trying a keyword list such as

["google.com": #PID<0.109.0>]

But Keyword.get only accepts atoms as a key to get the pid, so, I am a bit confused as to if it’s the way to go or not… is there a better way to get this to work?

Maybe you are looking for Map?

Keys can be either atoms or strings and to get the pids you use Map.values/1

And you can take the pid of a url using either map.key or Map.get(key)

1 Like

Maybe a Registry

1 Like

Yes you can use a registry which is something more advanced than plain data structure. For example you can’t use the simple Map.values function to get all your pids and you need to write an erlang spec to select them

I was about to say: Registry seems like a good fit for your problem.

Small callout - the : here makes the thing on the left side an atom, so this written without the keyword list sugar is:

[{:"google.com", pid}]

You likely don’t want hostnames turning into atoms, since atoms stick around forever.

The simplest alternative would be a Map, %{"google.com" => pid}

2 Likes