Skip to content

Commit

Permalink
Use PartitionSupervisor (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleDsz committed May 20, 2024
1 parent 42c4344 commit 5ce47c1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 4 additions & 3 deletions lib/kino_proxy/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ defmodule KinoProxy.Application do

@impl true
def start(_type, _args) do
children = [KinoProxy.Client]
opts = [strategy: :one_for_one, name: KinoProxy.Supervisor]
children = [
{PartitionSupervisor, child_spec: KinoProxy.Client, name: KinoProxy.PartitionSupervisor}
]

Supervisor.start_link(children, opts)
Supervisor.start_link(children, strategy: :one_for_one, name: KinoProxy.Supervisor)
end
end
13 changes: 10 additions & 3 deletions lib/kino_proxy/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ defmodule KinoProxy.Client do
# by the owner of the HTTP request.

use GenServer
@name Kino.Proxy

def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, opts, name: @name)
GenServer.start_link(__MODULE__, opts)
end

def listen(fun) when is_function(fun, 1) do
GenServer.cast(@name, {:listen, fun})
GenServer.cast(partition_name(), {:listen, fun})
end

def get_pid() do
GenServer.whereis(partition_name())
end

# GenServer callbacks
Expand All @@ -43,4 +46,8 @@ defmodule KinoProxy.Client do
def handle_cast({:listen, fun}, state) do
{:noreply, %{state | fun: fun}}
end

defp partition_name() do
{:via, PartitionSupervisor, {KinoProxy.PartitionSupervisor, Kino.Proxy}}
end
end
2 changes: 1 addition & 1 deletion test/support/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule KinoProxy.Endpoint do
script_name = [id, "proxy"]
conn = %{conn | path_info: path_info, script_name: conn.script_name ++ script_name}

if pid = GenServer.whereis(Kino.Proxy) do
if pid = KinoProxy.Client.get_pid() do
{conn, _reason} = Kino.Proxy.serve(pid, conn)
conn
else
Expand Down

0 comments on commit 5ce47c1

Please sign in to comment.