Page MenuHomePhorge

No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/majic/pool.ex b/lib/majic/pool.ex
index 079f50b..0c7ef0f 100644
--- a/lib/majic/pool.ex
+++ b/lib/majic/pool.ex
@@ -1,71 +1,75 @@
defmodule Majic.Pool do
@behaviour NimblePool
@moduledoc "Pool of `Majic.Server`"
@type name :: atom()
@type option :: {:pool_timeout, timeout()} | {:timeout, timeout()}
def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]},
type: :worker,
restart: :permanent,
shutdown: 500
}
end
def start_link(options) do
{pool_size, options} = Keyword.pop(options, :pool_size, System.schedulers_online())
NimblePool.start_link(worker: {__MODULE__, options}, pool_size: pool_size)
end
@spec perform(name(), Majic.target(), [option()]) :: Majic.result()
def perform(pool, path, opts \\ []) do
pool_timeout = Keyword.get(opts, :pool_timeout, Majic.Config.default_process_timeout())
timeout = Keyword.get(opts, :timeout, Majic.Config.default_process_timeout())
NimblePool.checkout!(
pool,
:checkout,
fn _, server ->
{Majic.Server.perform(server, path, timeout), server}
end,
pool_timeout
)
end
@impl NimblePool
def init_pool(options) do
{name, options} =
case Keyword.pop(options, :name) do
{name, options} when is_atom(name) -> {name, options}
{nil, options} -> {__MODULE__, options}
{_, options} -> {nil, options}
end
if name, do: Process.register(self(), name)
{:ok, options}
end
@impl NimblePool
def init_worker(options) do
{:ok, server} = Majic.Server.start_link(options || [])
{:ok, server, options}
end
@impl NimblePool
def handle_checkout(:checkout, _from, server, pool) do
{:ok, server, server, pool}
end
@impl NimblePool
def handle_checkin(_, _, server, pool) do
{:ok, server, pool}
end
@impl NimblePool
- def terminate_worker(_reason, _worker, state) do
+ def terminate_worker(_reason, server, state) do
+ if is_pid(server) and Process.alive?(server) do
+ Majic.Server.stop(server)
+ end
+
{:ok, state}
end
end
diff --git a/test/majic/pool_test.exs b/test/majic/pool_test.exs
index 38155e8..9c23c58 100644
--- a/test/majic/pool_test.exs
+++ b/test/majic/pool_test.exs
@@ -1,17 +1,27 @@
defmodule Majic.PoollTest do
use Majic.MagicCase
test "pool" do
{:ok, _} = Majic.Pool.start_link(name: TestPool, pool_size: 2)
assert {:ok, _} = Majic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = Majic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = Majic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = Majic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = Majic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = Majic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = Majic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = Majic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = Majic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = Majic.perform(absolute_path("Makefile"), pool: TestPool)
end
+
+ test "pool stops workers on termination" do
+ before = Process.list()
+ {:ok, pool} = Majic.Pool.start_link(pool_size: 2)
+ assert {:ok, _} = Majic.Pool.perform(pool, absolute_path("Makefile"))
+ GenServer.stop(pool)
+ after_stop = Process.list()
+ new_pids = after_stop -- before
+ assert new_pids == [], "Leaked processes after pool stop: #{inspect(new_pids)}"
+ end
end

File Metadata

Mime Type
text/x-diff
Expires
Sun, Aug 9, 10:43 AM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1722845
Default Alt Text
(3 KB)

Event Timeline