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: prevent client <-> db locking #195

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.22
0.9.23
16 changes: 8 additions & 8 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ defmodule Supavisor.ClientHandler do
@impl true
def callback_mode, do: [:handle_event_function]

def client_call(pid, bin, ready?) do
:gen_statem.call(pid, {:client_call, bin, ready?}, 5000)
def client_cast(pid, bin, ready?) do
:gen_statem.cast(pid, {:client_cast, bin, ready?})
end

@impl true
Expand Down Expand Up @@ -366,11 +366,11 @@ defmodule Supavisor.ClientHandler do
end
end

# emulate handle_call
def handle_event({:call, from}, {:client_call, bin, ready?}, _, data) do
# emulate handle_cast
def handle_event(:cast, {:client_cast, bin, ready?}, _, data) do
Logger.debug("--> --> bin #{inspect(byte_size(bin))} bytes")

reply = {:reply, from, HH.sock_send(data.sock, bin)}
:ok = HH.sock_send(data.sock, bin)

if ready? do
Logger.debug("Client is ready")
Expand All @@ -382,15 +382,15 @@ defmodule Supavisor.ClientHandler do

actions =
if data.idle_timeout > 0 do
[reply, idle_check(data.idle_timeout)]
idle_check(data.idle_timeout)
else
reply
[]
end

{:next_state, :idle, %{data | db_pid: db_pid, stats: stats}, actions}
else
Logger.debug("Client is not ready")
{:keep_state_and_data, reply}
:keep_state_and_data
end
end

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 @@ -21,7 +21,7 @@

@spec call(pid(), binary()) :: :ok | {:error, any()} | {:buffering, non_neg_integer()}
def call(pid, msg) do
:gen_statem.call(pid, {:db_call, msg})
:gen_statem.call(pid, {:db_call, msg}, 15_000)
end

@impl true
Expand Down Expand Up @@ -72,7 +72,7 @@

case try_ssl_handshake({:gen_tcp, sock}, auth) do
{:ok, sock} ->
case send_startup(sock, auth) do

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

View workflow job for this annotation

GitHub Actions / Formatting Checks

Function body is nested too deep (max depth is 2, was 3).
:ok ->
:ok = activate(sock)
{:next_state, :authentication, %{data | sock: sock}}
Expand Down Expand Up @@ -243,7 +243,7 @@
def handle_event(:info, {_proto, _, bin}, _, data) do
# check if the response ends with "ready for query"
ready = String.ends_with?(bin, Server.ready_for_query())
:ok = Client.client_call(data.caller, bin, ready)
:ok = Client.client_cast(data.caller, bin, ready)

if ready do
{_, stats} = Telem.network_usage(:db, data.sock, data.id, data.stats)
Expand Down
Loading