elbasti

elbasti

Hi folks, looking for some very basic beginner-level advice on architecture OTP applications.

I’m building a hobby-project application that receives external messages and then replies to them, and the replies depend on the conversation our application has had with that particular recipient.

The messages are received by a Phoenix application, and I’d like for a separate OTP application to be responsible for determining if/how to reply. The architecture I have in mind is:

  1. A DynamicSupervisor subscribes to a pubsub topic, :inbound_message_received.
  2. Upon receipt of the message, the supervisor does one of two things:
    a. Start a child GenServer that will then load the conversation we’ve had with that recipient as state, and then determine if/how to reply.
    b. Find an existing GenServer corresponding to that conversation, and then let it know a new message has arrived.

A few constraints:

  • Conversations are “bursty.” Sometimes there will be lots of back and forth, in which case it makes sense to keep the child servers alive, but they also go dormant, in which case it makes sense to kill them after a while until a new message is received.
  • The work done by the child servers can take a long time.

So the very first question I have is:

It seems kind of silly to have the supervisor listen to all new messages only to delegate them to the child servers that may or may not already exist. Naively, I think the children should subscribe to that topic as well–that way they can respond immediately if they already exist.

¿How would you handle that? ¿Can both a supervisor and a child of the supervisor subscribe to the same PubSub topic?

Showing Posts 1 to 6

kokolegorille

kokolegorille

Yes it’s possible

But what does the supervisor do, that cannot be solved by a registry?

elbasti

elbasti OP

The idea for the supervisor was that it would be responsible for starting a conversation’s GenServer in case the conversation was happening for the first time, or if that conversation’s GenServer had been killed/crashed/whatever.

jswanner

jswanner

I wouldn’t use a supervisor to listen to all messages and route them accordingly, as that supervisor will become a bottleneck.

Instead, I would write a function that was called from the client process (your Phoenix controller/liveview process) that was responsible for looking up the pid (or starting a new process) and calling it. That look up code would use something like GenServer.whereis with a :via tuple. With that setup, I wouldn’t need PubSub for the handling of incoming messages, but I would likely use PubSub for broadcasting out from the GenServer that a message had been received and processed.

kokolegorille

kokolegorille

Yes, something like this…

  def get_worker(name) do
    case WorkerSup.start_worker(name) do
      {:ok, worker} -> worker
      {:error, {:already_started, worker}} -> worker
      _response -> nil
    end
  end

This is what I meant to lookup via a Registry…

elbasti

elbasti OP

Ah, that makes sense, and that’s clearly the way to do it!

The original idea was to keep a very strict separation of concerns between the phoenix/webserver side and the “process messages” side. So the webserver would just broadcast “hey, I got a message!” and then it would be the part of the software that handles conversations that would figure out what to do with the message.

Would that then necessitate having a pool of supervisors so that they don’t become bottlenecks?

jswanner

jswanner

There’s a difference between separation of business logic concerns and separation of processes. Modules are for organizing functions, processes are for runtime concerns.

No, I don’t think that would be necessary. There’s already multiple client processes (controller/LiveView processes), by doing the process lookup from them the supervisor won’t be a bottleneck (the registry might, but I wouldn’t worry about that until you can prove it’s a problem). I think you want to focus more on how the code is organized into modules more than anything

— 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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews