Permalink
Browse files

introduce Registry

  • Loading branch information...
davidsulc committed Sep 8, 2018
1 parent f15811e commit e6705be153f86c396847c07e912b32ef28adb1c8
Showing with 19 additions and 3 deletions.
  1. +3 −0 lib/pool_toy.ex
  2. +2 −1 lib/pool_toy/application.ex
  3. +3 −0 lib/pool_toy/pool_sup.ex
  4. +9 −1 lib/pool_toy/pools_sup.ex
  5. +2 −1 mix.exs
View
@@ -1,3 +1,6 @@
defmodule PoolToy do
defdelegate start_pool(args), to: PoolToy.PoolsSup
defdelegate stop_pool(pool_name), to: PoolToy.PoolsSup
defdelegate checkout(pool), to: PoolToy.PoolMan
defdelegate checkin(pool, worker), to: PoolToy.PoolMan
end
@@ -3,10 +3,11 @@ defmodule PoolToy.Application do
def start(_type, _args) do
children = [
{Registry, [keys: :unique, name: PoolToy.Registry]},
PoolToy.PoolsSup
]
opts = [strategy: :one_for_one]
opts = [strategy: :rest_for_one]
Supervisor.start_link(children, opts)
end
end
View
@@ -6,6 +6,9 @@ defmodule PoolToy.PoolSup do
end
def init(args) do
pool_name = Keyword.fetch!(args, :name)
{:ok, _} = Registry.register(PoolToy.Registry, pool_name, self())
children = [
{PoolToy.PoolMan, [{:pool_sup, self()} | args]}
]
View
@@ -8,7 +8,15 @@ defmodule PoolToy.PoolsSup do
end
def start_pool(args) do
DynamicSupervisor.start_child(@name, {PoolToy.PoolSup, args})
case DynamicSupervisor.start_child(@name, {PoolToy.PoolSup, args}) do
{:ok, _} -> :ok
{:error, _} = error -> error
end
end
def stop_pool(pool_name) when is_atom(pool_name) do
[{_, pool_sup}] = Registry.lookup(PoolToy.Registry, pool_name)
DynamicSupervisor.terminate_child(@name, pool_sup)
end
def init(_opts) do
View
@@ -16,7 +16,8 @@ defmodule PoolToy.MixProject do
[
mod: {PoolToy.Application, []},
registered: [
PoolToy.PoolsSup
PoolToy.PoolsSup,
PoolToy.Registry
],
extra_applications: [:logger]
]

0 comments on commit e6705be

Please sign in to comment.