Skip to content

Commit

Permalink
fix: handle sync message when db_handler is busy (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 authored Jun 11, 2024
1 parent bb39c5d commit ed8840b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.59
1.1.60
7 changes: 7 additions & 0 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,13 @@ defmodule Supavisor.ClientHandler do
{:keep_state_and_data, handle_actions(data)}
end

def handle_event(:info, {proto, _, <<?S, 4::32>> = msg}, _, data)
when proto in [:tcp, :ssl] do
Logger.debug("ClientHandler: Receive sync while not idle")
Db.cast(data.db_pid, self(), msg)
:keep_state_and_data
end

# incoming query with a single pool
def handle_event(:info, {proto, _, bin}, :idle, %{pool: pid} = data)
when is_binary(bin) and is_pid(pid) do
Expand Down
13 changes: 13 additions & 0 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ defmodule Supavisor.DbHandler do
@spec call(pid(), pid(), binary()) :: :ok | {:error, any()} | {:buffering, non_neg_integer()}
def call(pid, caller, msg), do: :partisan_gen_statem.call(pid, {:db_call, caller, msg}, 15_000)

@spec cast(pid(), pid(), binary()) :: :ok | {:error, any()} | {:buffering, non_neg_integer()}
def cast(pid, caller, msg), do: :partisan_gen_statem.cast(pid, {:db_cast, caller, msg})

@spec get_state_and_mode(pid()) :: {:ok, {state, Supavisor.mode()}} | {:error, term()}
def get_state_and_mode(pid) do
try do
Expand Down Expand Up @@ -408,6 +411,16 @@ defmodule Supavisor.DbHandler do
{:keep_state, %{data | caller: caller, buffer: new_buff}, reply}
end

# emulate handle_cast
def handle_event(:cast, {:db_cast, caller, bin}, state, %{sock: sock}) do
Logger.debug(
"DbHandler: state #{state} <-- <-- bin #{inspect(byte_size(bin))} bytes, cast caller: #{inspect(caller)}"
)

sock_send(sock, bin)
:keep_state_and_data
end

def handle_event(_, {closed, _}, :busy, data) when closed in @sock_closed do
{:stop, :db_termination, data}
end
Expand Down

0 comments on commit ed8840b

Please sign in to comment.