dli
GenServer + DynamicSupervisor to track streaming workers per topic?
Hey there,
I am building a system that:
- streams data from an external HTTP API endpoint for multiple topics
- starts streaming processes under a supervisor
- allows runtime introspection which topics are currently streaming (optional: + which ones errored)
The external endpoint has a param for passing multiple topics (topic count per request is limited though). To stream additional topics, I need to make a new request.
Something like StreamerManager.start(topics) would launch one or more streamer processes for topics that are not streaming yet. Topics that are already streamed by some process are skipped. FWIW, I already implemented the Streamer + Consumers using GenStage but this is likely irrelevant to my question.
I am considering a GenServer that tracks %{:topic_name => pid()} in its state and calls DynamicSupervisor to launch streamers as child processes with the appropriate args. However, the PIDs will become invalid when a streamer crashes and is restarted by the supervisor (not sure if they even matter if I rely on a supervisor).
Am I off the track here or what would be the appropriate architecture here?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 7 of 7 Posts
shanesveller
You sound like you may be imagining something very similar to Registry and
via-tuples, will that save you some wheel reinvention? It automatically deregisters processes that terminate.It is single host only, not distributed, but scales very well IME especially if you’re on a new enough Elixir version to activate the partitioning support.
dli
Registry looks almost perfect for the job. Can’t believe I didn’t see this before.
With
via, I cannot register a process with multiple keys. I could use the topic list as a key, but then it would be hard to look up a single topic’s process without iterating through all keys.Would it make sense to do something like this? Anything that speaks against
registering inside aninitcallback? Having slight doubts about the coupling betweenStreamerandTopicRegistry, maybe not much of an issue though.shanesveller
I don’t see any concerns with this design off the top of my head, though in a standard GenServer I would possibly move the logic into a
handle_continueinstead of directly ininit.Tyson
My understanding is that any work done in
initwill block the Supervisor, so better to offload expensive setup tohandle_continue(which, unlike some earlier workarounds to that concern, is guaranteed to be the first message in the process mailbox).dli
This is not possible with GenStage
Edit: There is an open PR to GenStage that implements
:continueandhandle_continue: Add support for `handle_continue/2` callback by maartenvanvliet · Pull Request #300 · elixir-lang/gen_stage · GitHubTyson
Interesting. I haven’t had an opportunity to use GenStage yet. Then I guess
initwill have to do![edit: Yay for the
handle_continuePR, but looks like it’s been sitting there a while. And another PR before that for even longer!]dli
Here is what I am using now (thanks @shanesveller for the inspiration):
Start Registry in supervisor tree:
{Registry, keys: :unique, name: MyApp.TopicRegistry}When starting a
:producer/ “TopicStreamer” GenStage withtopics:Enum.rejectall topics that exist in set of keysMyApp.TopicStreamer.start_link(name: {:via, TopicRegistry, topics})This ensures that there are no
TopicStreamers with overlapping topics.