Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions integration_test/ownership/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,18 @@ defmodule ManagerTest do
end
end

test "includes label in closed connection error when label is provided" do
error = closed_connection_error(label: TestRepo)

assert error.message ==
"[TestRepo] connection is closed because of an error, disconnect or timeout"
end

test "doesn't require a label to produce a closed connection error" do
error = closed_connection_error([])
assert error.message == "connection is closed because of an error, disconnect or timeout"
end

test "unallow_existing cleans up old owner so its exit doesn't revoke the new allowance" do
stack = List.duplicate({:ok, :state}, 2) ++ List.duplicate({:idle, :state}, 20)
{:ok, agent} = A.start_link(stack)
Expand Down Expand Up @@ -775,6 +787,36 @@ defmodule ManagerTest do
{:ok, pid, opts}
end

defp closed_connection_error(opts) do
stack = [{:ok, :state}, {:idle, :state}, :ok, {:ok, :state}]
{:ok, agent} = A.start_link(stack ++ List.duplicate({:idle, :state}, 10))

opts = [agent: agent, parent: self(), ownership_mode: :manual] ++ opts
{:ok, pool} = P.start_link(opts)
:ok = Ownership.ownership_checkout(pool, ownership_timeout: 100)

capture_log(fn ->
P.run(
pool,
fn conn ->
# Hold the connection longer than the ownership_timeout above.
Process.sleep(200)

error =
assert_raise DBConnection.ConnectionError, fn ->
P.execute!(conn, %TestQuery{}, [:param], opts)
end

send(self(), {:closed_connection_error, error})
end,
opts
)
end)

assert_received {:closed_connection_error, error}
error
end

defp async_no_callers(fun) do
Task.async(fn ->
Process.delete(:"$callers")
Expand Down
2 changes: 1 addition & 1 deletion lib/db_connection/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ defmodule DBConnection.Connection do
end

defp pool_update(state, %{pool: pool, tag: tag, mod: mod, connected_at: connected_at} = s) do
case Holder.update(pool, tag, mod, state, connected_at) do
case Holder.update(pool, tag, mod, state, connected_at, s.opts[:label]) do
{:ok, ref} ->
{:keep_state, %{s | client: {ref, :pool}, state: state}, :hibernate}

Expand Down
41 changes: 28 additions & 13 deletions lib/db_connection/holder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ defmodule DBConnection.Holder do
:lock,
:connected_at,
deadline: nil,
status: :ok
status: :ok,
label: nil
])

Record.defrecord(:pool_ref, [:pool, :reference, :deadline, :holder, :lock])
Record.defrecord(:pool_ref, [:pool, :reference, :deadline, :holder, :lock, :label])

@type t :: :ets.tid()
@type checkin_time :: non_neg_integer() | nil
Expand All @@ -27,11 +28,20 @@ defmodule DBConnection.Holder do

@spec new(pid, reference, module, term) :: t
@spec new(pid, reference, module, term, integer) :: t
def new(pool, ref, mod, state, connected_at \\ System.monotonic_time()) do
@spec new(pid, reference, module, term, integer, term) :: t
def new(pool, ref, mod, state, connected_at \\ System.monotonic_time(), label \\ nil) do
# Insert before setting heir so that pool can't receive empty table
holder = :ets.new(__MODULE__, [:public, :ordered_set, decentralized_counters: true])

conn = conn(connection: self(), module: mod, state: state, connected_at: connected_at)
conn =
conn(
connection: self(),
module: mod,
state: state,
connected_at: connected_at,
label: label
)

true = :ets.insert_new(holder, conn)

:ets.setopts(holder, {:heir, pool, ref})
Expand All @@ -40,8 +50,9 @@ defmodule DBConnection.Holder do

@spec update(pid, reference, module, term) :: {:ok, t} | :error
@spec update(pid, reference, module, term, integer) :: {:ok, t} | :error
def update(pool, ref, mod, state, connected_at \\ System.monotonic_time()) do
holder = new(pool, ref, mod, state, connected_at)
@spec update(pid, reference, module, term, integer, term) :: {:ok, t} | :error
def update(pool, ref, mod, state, connected_at \\ System.monotonic_time(), label \\ nil) do
holder = new(pool, ref, mod, state, connected_at, label)

try do
:ets.give_away(holder, pool, {:checkin, ref, System.monotonic_time()})
Expand Down Expand Up @@ -142,21 +153,19 @@ defmodule DBConnection.Holder do
end

defp handle_or_cleanup(type, pool_ref, fun, args, opts) do
pool_ref(holder: holder, lock: lock) = pool_ref
pool_ref(holder: holder, lock: lock, label: label) = pool_ref

try do
:ets.lookup(holder, :conn)
rescue
ArgumentError ->
msg = "connection is closed because of an error, disconnect or timeout"
{:disconnect, DBConnection.ConnectionError.exception(msg), _state = :unused}
{:disconnect, closed_error(label), _state = :unused}
else
[conn(lock: conn_lock)] when conn_lock != lock ->
raise "an outdated connection has been given to DBConnection on #{fun}/#{length(args) + 2}"

[conn(status: :error)] ->
msg = "connection is closed because of an error, disconnect or timeout"
{:disconnect, DBConnection.ConnectionError.exception(msg), _state = :unused}
{:disconnect, closed_error(label), _state = :unused}

[conn(status: :aborted)] when type != :cleanup ->
msg = "transaction rolling back"
Expand Down Expand Up @@ -345,12 +354,18 @@ defmodule DBConnection.Holder do
# Deadline could hit and be handled by the pool before using the connection.
{:error, checkout_deadline_error()}
else
[conn(module: mod, state: state)] ->
{:ok, pool_ref, mod, checkin_time, state}
[conn(module: mod, state: state, label: label)] ->
{:ok, pool_ref(pool_ref, label: label), mod, checkin_time, state}
end
end
end

defp closed_error(label) do
msg = "connection is closed because of an error, disconnect or timeout"
msg = if label == nil, do: msg, else: "[#{inspect(label)}] " <> msg
DBConnection.ConnectionError.exception(msg)
end

defp checkout_deadline_error do
DBConnection.ConnectionError.exception(
"connection not available because deadline reached while in queue"
Expand Down
5 changes: 4 additions & 1 deletion lib/db_connection/ownership/proxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule DBConnection.Ownership.Proxy do
@moduledoc false

alias DBConnection.Holder
require Holder
alias DBConnection.Util
use GenServer, restart: :temporary

Expand Down Expand Up @@ -109,7 +110,9 @@ defmodule DBConnection.Ownership.Proxy do
{:ok, pool_ref, original_mod, _idle_time, conn_state} ->
case post_checkout.(original_mod, conn_state) do
{:ok, conn_mod, conn_state} ->
holder = Holder.new(self(), owner_ref, conn_mod, conn_state)
label = Holder.pool_ref(pool_ref, :label)
now = System.monotonic_time()
holder = Holder.new(self(), owner_ref, conn_mod, conn_state, now, label)
state = %{state | pool_ref: pool_ref, holder: holder, mod: original_mod}
checkout(from, state)

Expand Down