Pool: format & doc

This commit is contained in:
Jordan Bracco 2020-05-13 21:29:48 +02:00
parent 77dfe98011
commit eec13a033b
3 changed files with 44 additions and 14 deletions

View file

@ -101,9 +101,23 @@ Note that in this case we have opted to use a named process.
For concurrency *and* resiliency, you may start the `GenMagic.Pool`. By default, it will start a `GenMagic.Server` For concurrency *and* resiliency, you may start the `GenMagic.Pool`. By default, it will start a `GenMagic.Server`
worker per online scheduler: worker per online scheduler:
```elixir You can add a pool in your application supervisor by adding it as a child:
iex(1)> GenMagic.Pool.start_link([])
iex(2)> GenMagic.Pool.perform(GenMagic.Pool, Path.expand("~/.bash_history")) ```
children =
[
# ...
{GenMagic.Pool, [name: YourApp.GenMagicPool, pool_size: 2]}
]
opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
Supervisor.start_link(children, opts)
```
And then you can use it with `GenMagic.Pool.perform/2`:
```
iex(1)> GenMagic.Pool.perform(YourApp.GenMagicPool, Path.expand("~/.bash_history"))
{:ok, [mime_type: "text/plain", encoding: "us-ascii", content: "ASCII text"]} {:ok, [mime_type: "text/plain", encoding: "us-ascii", content: "ASCII text"]}
``` ```

View file

@ -1,5 +1,16 @@
defmodule GenMagic.Pool do defmodule GenMagic.Pool do
@behaviour NimblePool @behaviour NimblePool
@moduledoc "Pool of `GenMagic.Server`"
def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]},
type: :worker,
restart: :permanent,
shutdown: 500
}
end
def start_link(options, pool_size \\ nil) do def start_link(options, pool_size \\ nil) do
pool_size = pool_size || System.schedulers_online() pool_size = pool_size || System.schedulers_online()
@ -10,18 +21,25 @@ defmodule GenMagic.Pool do
pool_timeout = Keyword.get(opts, :pool_timeout, 5000) pool_timeout = Keyword.get(opts, :pool_timeout, 5000)
timeout = Keyword.get(opts, :timeout, 5000) timeout = Keyword.get(opts, :timeout, 5000)
NimblePool.checkout!(pool, :checkout, fn _, server -> NimblePool.checkout!(
pool,
:checkout,
fn _, server ->
{GenMagic.Server.perform(server, path, timeout), server} {GenMagic.Server.perform(server, path, timeout), server}
end, pool_timeout) end,
pool_timeout
)
end end
@impl NimblePool @impl NimblePool
def init_pool(options) do def init_pool(options) do
{name, options} = case Keyword.pop(options, :name) do {name, options} =
case Keyword.pop(options, :name) do
{name, options} when is_atom(name) -> {name, options} {name, options} when is_atom(name) -> {name, options}
{nil, options} -> {__MODULE__, options} {nil, options} -> {__MODULE__, options}
{_, options} -> {nil, options} {_, options} -> {nil, options}
end end
if name, do: Process.register(self(), name) if name, do: Process.register(self(), name)
{:ok, options} {:ok, options}
end end
@ -46,5 +64,4 @@ defmodule GenMagic.Pool do
def terminate_worker(_reason, _worker, state) do def terminate_worker(_reason, _worker, state) do
{:ok, state} {:ok, state}
end end
end end

View file

@ -2,7 +2,7 @@ defmodule GenMagic.PoollTest do
use GenMagic.MagicCase use GenMagic.MagicCase
test "pool" do test "pool" do
{:ok, _} = GenMagic.Pool.start_link([name: TestPool, pool_size: 2]) {:ok, _} = GenMagic.Pool.start_link(name: TestPool, pool_size: 2)
assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile")) assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile")) assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile")) assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile"))
@ -13,5 +13,4 @@ defmodule GenMagic.PoollTest do
assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile")) assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile"))
assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile")) assert {:ok, _} = GenMagic.Pool.perform(TestPool, absolute_path("Makefile"))
end end
end end