Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleanly shutdown DbHandler on failure #497

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 15 additions & 2 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@

key = {:secrets, tenant_or_alias, user}

case auth_secrets(info, user, key, :timer.hours(24)) do

Check warning on line 280 in lib/supavisor/client_handler.ex

View workflow job for this annotation

GitHub Actions / Code style

Function body is nested too deep (max depth is 2, was 3).
{:ok, auth_secrets} ->
Logger.debug("ClientHandler: Authentication method: #{inspect(auth_secrets)}")

Expand Down Expand Up @@ -336,7 +336,7 @@
Cachex.get(Supavisor.Cache, key) == {:ok, nil} do
case auth_secrets(info, data.user, key, 15_000) do
{:ok, {method2, secrets2}} = value ->
if method != method2 or Map.delete(secrets.(), :client_key) != secrets2.() do

Check warning on line 339 in lib/supavisor/client_handler.ex

View workflow job for this annotation

GitHub Actions / Code style

Function body is nested too deep (max depth is 2, was 4).
Logger.warning("ClientHandler: Update secrets and terminate pool")

Cachex.update(
Expand Down Expand Up @@ -469,7 +469,20 @@
}

{:ok, db_pid} = DbHandler.start_link(args)
db_sock = :gen_statem.call(db_pid, {:checkout, data.sock, self()})

db_sock =
try do
DbHandler.checkout(db_pid, data.sock)
rescue
err ->
Logger.error(
"ClientHandler: Error while connecting to the DB - #{Exception.message(err)}"
)

_ = DbHandler.stop(db_pid)
reraise err, __STACKTRACE__
hauleth marked this conversation as resolved.
Show resolved Hide resolved
end

{:keep_state, %{data | db_pid: {nil, db_pid, db_sock}, mode: :proxy}}
end

Expand Down Expand Up @@ -769,7 +782,7 @@
start = System.monotonic_time(:microsecond)
db_pid = :poolboy.checkout(data.pool, true, data.timeout)
Process.link(db_pid)
db_sock = DbHandler.checkout(db_pid, data.sock, self())
db_sock = DbHandler.checkout(db_pid, data.sock)
same_box = if node(db_pid) == node(), do: :local, else: :remote
Telem.pool_checkout_time(System.monotonic_time(:microsecond) - start, data.id, same_box)
{data.pool, db_pid, db_sock}
Expand Down
4 changes: 2 additions & 2 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
def start_link(config),
do: :gen_statem.start_link(__MODULE__, config, hibernate_after: 5_000)

def checkout(pid, sock, caller, timeout \\ 15_000),
do: :gen_statem.call(pid, {:checkout, sock, caller}, timeout)
def checkout(pid, sock, timeout \\ 15_000),
do: :gen_statem.call(pid, {:checkout, sock, self()}, timeout)

@spec checkin(pid()) :: :ok
def checkin(pid), do: :gen_statem.cast(pid, :checkin)
Expand Down Expand Up @@ -125,7 +125,7 @@
tenant = if data.proxy, do: Supavisor.tenant(data.id)
search_path = Supavisor.search_path(data.id)

case send_startup(sock, auth, tenant, search_path) do

Check warning on line 128 in lib/supavisor/db_handler.ex

View workflow job for this annotation

GitHub Actions / Code style

Function body is nested too deep (max depth is 2, was 3).
:ok ->
:ok = activate(sock)
{:next_state, :authentication, %{data | sock: sock}}
Expand Down
Loading