Use a registry for nts clients
This commit is contained in:
parent
d726089705
commit
06f165fc36
|
@ -15,8 +15,9 @@ defmodule Chronoscope.Application do
|
||||||
{Finch, name: Chronoscope.Finch},
|
{Finch, name: Chronoscope.Finch},
|
||||||
# Start a worker by calling: Chronoscope.Worker.start_link(arg)
|
# Start a worker by calling: Chronoscope.Worker.start_link(arg)
|
||||||
# {Chronoscope.Worker, arg},
|
# {Chronoscope.Worker, arg},
|
||||||
{DynamicSupervisor, name: Chronoscope.DynamicSupervisor, strategy: :one_for_one},
|
{DynamicSupervisor, name: Chronoscope.NTS.DynamicSupervisor, strategy: :one_for_one},
|
||||||
{Task.Supervisor, name: Chronoscope.TaskSupervisor},
|
{Task.Supervisor, name: Chronoscope.NTS.TaskSupervisor},
|
||||||
|
{Registry, [keys: :unique, name: Chronoscope.NTS.Registry]},
|
||||||
# Start to serve requests, typically the last entry
|
# Start to serve requests, typically the last entry
|
||||||
ChronoscopeWeb.Endpoint
|
ChronoscopeWeb.Endpoint
|
||||||
]
|
]
|
||||||
|
|
|
@ -14,19 +14,26 @@ defmodule Chronoscope.NTS do
|
||||||
end
|
end
|
||||||
|
|
||||||
def list() do
|
def list() do
|
||||||
Chronoscope.DynamicSupervisor
|
NTS.DynamicSupervisor
|
||||||
|> DynamicSupervisor.which_children()
|
|> DynamicSupervisor.which_children()
|
||||||
|> Enum.map(fn {_, pid, _, _} -> GenServer.call(pid, :list) end)
|
|> Enum.map(fn {_, pid, _, _} -> GenServer.call(pid, :list) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def key_establishment(host, port) do
|
def key_establishment(host, port) do
|
||||||
# todo don't use atoms for this
|
name = "#{host}:#{port}"
|
||||||
name = String.to_atom("#{host}:#{port}")
|
|
||||||
|
|
||||||
if GenServer.whereis(name) == nil do
|
case Registry.lookup(NTS.Registry, name) do
|
||||||
DynamicSupervisor.start_child(Chronoscope.DynamicSupervisor, {NTS.Client, host: host, port: port, name: name})
|
[{pid, _}] ->
|
||||||
|
GenServer.call(pid, :key_establishment)
|
||||||
|
|
||||||
|
[] ->
|
||||||
|
NTS.DynamicSupervisor
|
||||||
|
|> DynamicSupervisor.start_child({
|
||||||
|
NTS.Client,
|
||||||
|
host: host, port: port, name: {:via, Registry, {NTS.Registry, name}}
|
||||||
|
})
|
||||||
|
|
||||||
|
key_establishment(host, port)
|
||||||
end
|
end
|
||||||
|
|
||||||
GenServer.call(name, :key_establishment)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
defmodule Chronoscope.NTS.Client do
|
defmodule Chronoscope.NTS.Client do
|
||||||
use GenServer
|
use GenServer
|
||||||
|
|
||||||
|
alias Chronoscope.NTS
|
||||||
alias Chronoscope.NTS.KeyEstablishmentClient
|
alias Chronoscope.NTS.KeyEstablishmentClient
|
||||||
|
|
||||||
@interval_in_seconds 30
|
@interval_in_seconds 30
|
||||||
|
@ -54,7 +55,7 @@ defmodule Chronoscope.NTS.Client do
|
||||||
defp server_response(state) do
|
defp server_response(state) do
|
||||||
server = Map.take(state, [:host, :port])
|
server = Map.take(state, [:host, :port])
|
||||||
|
|
||||||
Chronoscope.TaskSupervisor
|
NTS.TaskSupervisor
|
||||||
|> Task.Supervisor.async(fn -> KeyEstablishmentClient.key_establishment(server) end)
|
|> Task.Supervisor.async(fn -> KeyEstablishmentClient.key_establishment(server) end)
|
||||||
|> Task.await()
|
|> Task.await()
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue