zeroexcuses

zeroexcuses

  1. I am looking for something more useful than “just use Phoenix.PubSub”

  2. I understand there are various security issues. For our definition of ‘minimal’, let us consider the following operations:

control: HashMap<Channel, HashSet<Client>>

fn send_msg(c: Channel, msg: Msg) {
  for client in control.get(c) {
    client.send(msg);
  }
}

fn add_channel(c: Channel) {
  control.insert(c);
}

fn del_channel(c: Channel) {
  send_msg(c, "exit");
  control.delete(c);
}

fn add_client(c: Channel, client: Client) {
  control[c].insert(client);
}

fn del_client(c: Channel, client: Client) {
  control[c].del(client);
}
  1. Now, here is the question. How do we make this distributed ? How do we shard the ‘control’? How do messages get routed to the right place?

I am looking for things on the level of pseudocode/working-code (not informal English descriptions).

Any recommendations for tutorials / books that works through the design / implementation of such a system ?

Showing Posts 1 to 5

dorgan

dorgan

I’d recommend checking out the erlang :pg module, which is what Phoenix PubSub uses under the hood in one of it’s adapters.
This module implements distributed process groups. A process can subscribe to a group(which could be your topic, following the phoenix pubsub analogy), and you can fetch all the members of a group and send them a message to do a broadcast. The groups list is managed by :pg itself, so you don’t have to worry about adding them and doing cleanup.

A minimal implementation of your use case would be:

defmodule PubSub do

  @type channel :: any
  @type client :: pid

  @spec send_msg(channel, any) :: :ok
  def send_msg(channel, msg) do
    for client <- :pg.get_members(channel) do
      send(client, msg)
    end
    :ok
  end

  @spec add_client(channel, client) :: :ok
  def add_client(channel, client) do
    :pg.join(channel, pid)
  end

  @spec del_client(channel, client) :: :ok
  def del_client(channel, client) do
    :pg.leave(channel, client)
  end
end

Note that add_channel and del_channel are not needed, as that’s handled automatically by :pg.
Distribution is handled automatically by Elixir, since you can send messages between nodes and it’s managed by erlang’s distribution.

If what you want is to implement something like that from scratch, then you’d need to implement the channels bookkeeping and whatever kind of consistency guarantees you need, as the rest is just regular message passing. I have never done this myself so I don’t have any code snippets to share.

zeroexcuses

zeroexcuses OP

For anyone curious: otp/lib/kernel/src/pg.erl at master · erlang/otp · GitHub – this looks really helpful. Thanks!

zeroexcuses

zeroexcuses OP

If someone more versed in Erlang/OTP than me could jump in, where exactly in otp/lib/kernel/src/pg.erl at master · erlang/otp · GitHub (which line) is the ‘HashMap<Channel, HashSet>’ stored ?

lud

lud

I guess it is implemented using ETS:

join_local_group(Scope, Group, Pid) when is_pid(Pid) ->
    case ets:lookup(Scope, Group) of
        [{Group, All, Local}] ->
            ets:insert(Scope, {Group, [Pid | All], [Pid | Local]});
        [] ->
            ets:insert(Scope, {Group, [Pid], [Pid]})
max-au

max-au

In pg, ETS is only a cache to circumvent message passing, and speed up get_members while making it really concurrent.

Source of truth - map “Group => [Pid1, Pid2, Pid3, …]” is stored as a part of pg process state: otp/lib/kernel/src/pg.erl at master · erlang/otp · GitHub

Since pg is expected to be used for highly distributed use-cases, this map is added per “scope process”, implementing overlay networks (see some older documentation here: spg/README.md at master · max-au/spg · GitHub )

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
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
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews