Hello everyone!
I’m new to Elixir and I’m trying to use broadway with Mongo multi-tenant.
Every time I get a different host, I need to open a connection to Mongo, but I notice that I have more than one connection per host.
Before opening the connection, I validate if the process has started, but I’m not sure if it’s correct.
defmodule CoreConsumer.Shared.Mongo.Tenant do
use DynamicSupervisor
require Logger
alias FE.Result
alias CoreConsumer.Shared.Mongo
def start_link(_arg) do
DynamicSupervisor.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(:ok) do
DynamicSupervisor.init(strategy: :one_for_one)
end
def start_connection(host) do
case Process.whereis(:"#{host}") do
nil ->
DynamicSupervisor.start_child(__MODULE__, %{
id: host,
type: :worker,
restart: :transient,
start: {Mongo, :start_link, [[host: host]]}
})
pid ->
Result.ok(pid)
end
end
end