Created
November 9, 2021 12:30
-
-
Save psylone/59c971d3766889441e84d1b2c5e3349b to your computer and use it in GitHub Desktop.
Using poolboy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mix.install([ | |
{:poolboy, ''} | |
]) | |
defmodule Pool do | |
@pool_name Macro.underscore(__MODULE__) | |
|> String.to_atom() | |
def child_spec(opts) do | |
:poolboy.child_spec( | |
__MODULE__, | |
poolboy_config, | |
opts | |
) | |
end | |
def test do | |
:poolboy.transaction(@pool_name, fn pid -> | |
GenServer.call(pid, :test) | |
end) | |
end | |
defp poolboy_config do | |
[ | |
name: {:local, @pool_name}, | |
worker_module: Sample | |
] | |
end | |
end | |
defmodule Sample do | |
@moduledoc """ | |
Documentation for `Sample`. | |
""" | |
use GenServer | |
def start_link(opts) do | |
IO.inspect(opts, label: "start_link arg") | |
GenServer.start_link(__MODULE__, %{}, opts) | |
end | |
@impl true | |
def init(init_arg) do | |
{:ok, init_arg} | |
end | |
@impl true | |
def handle_call(:test, _from, state) do | |
{:reply, :ok, state} | |
end | |
end | |
children = [Pool] | |
opts = [strategy: :one_for_one, name: Sample.Supervisor] | |
Supervisor.start_link(children, opts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment