pool.ex (907B)
1 defmodule DBConnection.ConnectionPool.Pool do 2 @moduledoc false 3 use Supervisor, restart: :temporary 4 5 def start_supervised(tag, mod, opts) do 6 DBConnection.Watcher.watch( 7 DBConnection.ConnectionPool.Supervisor, 8 {DBConnection.ConnectionPool.Pool, {self(), tag, mod, opts}} 9 ) 10 end 11 12 def start_link(arg) do 13 Supervisor.start_link(__MODULE__, arg) 14 end 15 16 def init({owner, tag, mod, opts}) do 17 size = Keyword.get(opts, :pool_size, 1) 18 children = for id <- 1..size, do: conn(owner, tag, id, mod, opts) 19 sup_opts = [strategy: :one_for_one] ++ Keyword.take(opts, [:max_restarts, :max_seconds]) 20 Supervisor.init(children, sup_opts) 21 end 22 23 ## Helpers 24 25 defp conn(owner, tag, id, mod, opts) do 26 child_opts = [id: {mod, owner, id}] ++ Keyword.take(opts, [:shutdown]) 27 DBConnection.Connection.child_spec(mod, [pool_index: id] ++ opts, owner, tag, child_opts) 28 end 29 end