Permalink
Browse files

remove static process names

  • Loading branch information...
davidsulc committed Sep 2, 2018
1 parent 953fc29 commit 4a706248a685b158166e85a7025ef7a7f11eb2f1
Showing with 69 additions and 34 deletions.
  1. +1 −1 lib/pool_toy/application.ex
  2. +65 −18 lib/pool_toy/pool_man.ex
  3. +2 −7 lib/pool_toy/pool_sup.ex
  4. +1 −3 lib/pool_toy/worker_sup.ex
  5. +0 −5 mix.exs
@@ -3,7 +3,7 @@ defmodule PoolToy.Application do
def start(_type, _args) do
children = [
{PoolToy.PoolSup, [size: 3]}
{PoolToy.PoolSup, [name: :poolio, size: 3]}
]
opts = [strategy: :one_for_one]
View
@@ -3,32 +3,68 @@ defmodule PoolToy.PoolMan do
defmodule State do
defstruct [
:size, :monitors,
worker_sup: PoolToy.WorkerSup, worker_spec: Doubler, workers: []
:name, :size, :pool_sup,
:monitors, :worker_sup,
worker_spec: Doubler, workers: []
]
end
@name __MODULE__
def start_link(args) do
name = Keyword.fetch!(args, :name)
GenServer.start_link(__MODULE__, args, name: name)
end
def checkout(pool) do
GenServer.call(pool, :checkout)
end
def checkin(pool, worker) do
GenServer.cast(pool, {:checkin, worker})
end
def init(args), do: init(args, %State{})
defp init([{:name, name} | rest], %State{} = state) when is_atom(name) do
init(rest, %{state | name: name})
end
defp init([{:name, _name} | _], _state) do
{:stop, {:invalid_args, {:name, "must be an atom"}}}
end
defp init([{:size, size} | rest], %State{} = state) when is_integer(size) and size > 0 do
init(rest, %{state | size: size})
end
defp init([{:size, _size} | _], _state) do
{:stop, {:invalid_args, {:size, "must be a positive integer"}}}
end
defp init([{:pool_sup, pid} | rest], %State{} = state) when is_pid(pid) do
init(rest, %{state | pool_sup: pid})
end
def start_link(size) when is_integer(size) and size > 0 do
GenServer.start_link(__MODULE__, size, name: @name)
defp init([{:pool_sup, _} | _], _state) do
{:stop, {:invalid_args, {:pool_sup, "must be provided"}}}
end
def checkout() do
GenServer.call(@name, :checkout)
defp init([], %State{name: nil}) do
{:stop, {:missing_args, {:name, "atom `name` is required"}}}
end
def checkin(worker) do
GenServer.cast(@name, {:checkin, worker})
defp init([], %State{size: nil}) do
{:stop, {:missing_args, {:size, "positive integer `size` is required"}}}
end
def init(size) do
defp init([], %State{name: _, size: _} = state) do
Process.flag(:trap_exit, true)
send(self(), :start_workers)
send(self(), :start_worker_sup)
monitors = :ets.new(:monitors, [:protected, :named_table])
{:ok, %State{size: size, monitors: monitors}}
{:ok, %{state | monitors: monitors}}
end
defp init([_ | t], state), do: init(t, state)
def handle_call(:checkout, _from, %State{workers: []} = state) do
{:reply, :full, state}
end
@@ -50,13 +86,15 @@ defmodule PoolToy.PoolMan do
end
end
def handle_info(:start_workers, %State{worker_sup: sup, worker_spec: spec, size: size} = state) do
workers =
for _ <- 1..size do
new_worker(sup, spec)
end
def handle_info(:start_worker_sup, %State{pool_sup: sup} = state) do
{:ok, worker_sup} = Supervisor.start_child(sup, PoolToy.WorkerSup)
{:noreply, %{state | workers: workers}}
state =
state
|> Map.put(:worker_sup, worker_sup)
|> start_workers()
{:noreply, state}
end
def handle_info({:DOWN, ref, :process, _, _}, %State{monitors: monitors} = state) do
@@ -89,6 +127,15 @@ defmodule PoolToy.PoolMan do
{:noreply, state}
end
def start_workers(%State{worker_sup: sup, worker_spec: spec, size: size} = state) do
workers =
for _ <- 1..size do
new_worker(sup, spec)
end
%{state | workers: workers}
end
defp new_worker(sup, spec) do
child_spec = Supervisor.child_spec(spec, restart: :temporary)
{:ok, pid} = PoolToy.WorkerSup.start_worker(sup, child_spec)
View
@@ -1,18 +1,13 @@
defmodule PoolToy.PoolSup do
use Supervisor
@name __MODULE__
def start_link(args) when is_list(args) do
Supervisor.start_link(__MODULE__, args, name: @name)
Supervisor.start_link(__MODULE__, args)
end
def init(args) do
pool_size = args |> Keyword.fetch!(:size)
children = [
PoolToy.WorkerSup,
{PoolToy.PoolMan, pool_size}
{PoolToy.PoolMan, [{:pool_sup, self()} | args]}
]
Supervisor.init(children, strategy: :one_for_all)
@@ -1,10 +1,8 @@
defmodule PoolToy.WorkerSup do
use DynamicSupervisor
@name __MODULE__
def start_link(args) do
DynamicSupervisor.start_link(__MODULE__, args, name: @name)
DynamicSupervisor.start_link(__MODULE__, args)
end
defdelegate start_worker(sup, spec), to: DynamicSupervisor, as: :start_child
View
@@ -15,11 +15,6 @@ defmodule PoolToy.MixProject do
def application do
[
mod: {PoolToy.Application, []},
registered: [
PoolToy.PoolSup,
PoolToy.PoolMan,
PoolToy.WorkerSup
],
extra_applications: [:logger]
]
end

0 comments on commit 4a70624

Please sign in to comment.