dli

dli

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?

Showing Posts 1 to 7

shanesveller

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

dli OP

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 an init callback? Having slight doubts about the coupling between Streamer and TopicRegistry, maybe not much of an issue though.

defmodule Streamer do
  use GenStage

  def init(topics) do
    for topic <- topics do
      # value does not matter at the moment
      Registry.register(MyApp.TopicRegistry, topic, nil)
    end

    # ...
    {:producer, ...}
  end
end
shanesveller

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_continue instead of directly in init.

Tyson

Tyson

:+1:

My understanding is that any work done in init will block the Supervisor, so better to offload expensive setup to handle_continue (which, unlike some earlier workarounds to that concern, is guaranteed to be the first message in the process mailbox).

dli

dli OP

offload expensive setup to handle_continue

This is not possible with GenStage :frowning:

Edit: There is an open PR to GenStage that implements :continue and handle_continue: Add support for `handle_continue/2` callback by maartenvanvliet · Pull Request #300 · elixir-lang/gen_stage · GitHub

Tyson

Tyson

Interesting. I haven’t had an opportunity to use GenStage yet. Then I guess init will have to do!

[edit: Yay for the handle_continue PR, but looks like it’s been sitting there a while. And another PR before that for even longer!]

dli

dli OP

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 with topics:

    • fetch all keys from Registry:
      Registry.select(MyApp.TopicRegistry, [{{:"$1", :_, :_}, [], [:"$1"]}])
      |> List.flatten()
      |> MapSet.new()
      
    • Enum.reject all topics that exist in set of keys
    • MyApp.TopicStreamer.start_link(name: {:via, TopicRegistry, topics})

This ensures that there are no TopicStreamers with overlapping topics.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews