I came across the need for functionality like this when I was setting up an architecture where an outside source, a list of keys, should be used to determine what processes to run. For each key in that list a process should be started. Over time, the list will change, and the processes running should be adjusted to represent this. Assuming there's a DynamicSupervisor owning the workers and a coordinating process starting and stopping them, I need a list of the current running processes and their keys to compare with the latest version of the external state.
A simple solution to the problem would be to rely on the changes between the latest version of the list and the last version, and trust the supervisor to restart dying processes. This, however, means the workers can't be `:temporary`, because we would risk reality getting out of sync of the list of workers on the coordinator. But even if I go for `:transient` workers, I still risk getting out of sync if the DynamicSupervisor crashes. Even if I keep that process clean to avoid crashes, some worker crashing repeatedly can still bring it down, and all the workers with it. At that point, the only way to synchronize back to the correct state would be restarting the server or implement some complex code where the coordinator attempts to verify the status of each key by looking it up in the registry, but then we're back to inefficiently getting all keys.
I propose a function that looks and works something like this (naming and ordering of return values is obviously up for discussion)
```
Registry.all(MyRegistry)
=> [{key, pid, value}, {key, pid, value}, ...]
```
Just like `Registry.count/1` has a corresponding `Registry.count_match/{3,4}`, it could be designed to take a match specification.
```
handle_info(:interval, state) do
new_keys = get_keys_from_external()
Registry.all(registry, fn entries ->
current_keys = Enum.map(entries, &(elem(&1, 0))
synchronize(current_keys, new_keys)
end)
end
```
Which is okay, I guess, but the data is stuck in the callback. Also, if we copy the way that dispatch handles partitions the code would only work if there aren't multiple partitions. Not sure how I'd go about collecting the entries from multiple partitions, maybe in an ETS table? Sending to myself and storing in state? Either way, it's not very ergonomic. Maybe I'm missing an obvious solution.
Would it make sense to implement it as a Stream? It should provide some of the same properties as the dispatch solution. We could iterate over the tables and handle partitions seamlessly.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/503ef13f-9e18-4e64-8e1c-84ca677ab504%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JO%3DjveXLf5RHyGqsnNVnjpLW1%2BsDV%2BHpRv8LD9ZwWkkw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JO%3DjveXLf5RHyGqsnNVnjpLW1%2BsDV%2BHpRv8LD9ZwWkkw%40mail.gmail.com.
As it seems there's at least consensus on adding this feature (if not exactly on how it should work) would it be reasonable for me to attempt a PR for this? I could start out by implementing to_list at least, it should be straightforward.
I'm unsure of the practicalities, should I add an issue to the Github repo first?
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/57494517-aaa9-4dd6-97d5-bf5fa0089922%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4Lt989nD909VBXrVpCdO3qdRVyafSLbzvYhc6bUwsGEvA%40mail.gmail.com.