diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index f2da2f26..50fa5209 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -303,7 +303,7 @@ defmodule Supavisor.ClientHandler do end def handle_event(_, {proto, _, bin}, :busy, data) when proto in [:tcp, :ssl] do - case Db.call(data.db_pid, bin, self()) do + case Db.call(data.db_pid, self(), bin) do :ok -> Logger.debug("DB call success") :keep_state_and_data diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index 20570365..1a932a4e 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -19,9 +19,9 @@ defmodule Supavisor.DbHandler do :gen_statem.start_link(__MODULE__, config, hibernate_after: 5_000) end - @spec call(pid(), binary(), pid()) :: :ok | {:error, any()} | {:buffering, non_neg_integer()} - def call(pid, msg, caller) do - :gen_statem.call(pid, {:db_call, msg, caller}, 15_000) + @spec call(pid(), pid(), binary()) :: :ok | {:error, any()} | {:buffering, non_neg_integer()} + def call(pid, caller, msg) do + :gen_statem.call(pid, {:db_call, caller, msg}, 15_000) end @impl true @@ -254,12 +254,12 @@ defmodule Supavisor.DbHandler do end end - def handle_event({:call, from}, {:db_call, bin, caller}, :idle, %{sock: sock} = data) do + def handle_event({:call, from}, {:db_call, caller, bin}, :idle, %{sock: sock} = data) do reply = {:reply, from, sock_send(sock, bin)} {:keep_state, %{data | caller: caller}, reply} end - def handle_event({:call, from}, {:db_call, bin, caller}, state, %{buffer: buff} = data) do + def handle_event({:call, from}, {:db_call, caller, bin}, state, %{buffer: buff} = data) do Logger.debug( "state #{state} <-- <-- bin #{inspect(byte_size(bin))} bytes, caller: #{inspect(caller)}" ) diff --git a/test/supavisor/db_handler_test.exs b/test/supavisor/db_handler_test.exs index a75a573f..4b2ed693 100644 --- a/test/supavisor/db_handler_test.exs +++ b/test/supavisor/db_handler_test.exs @@ -97,7 +97,7 @@ defmodule Supavisor.DbHandlerTest do data = %{sock: {:gen_tcp, sock}, caller: nil, buffer: []} from = {self(), :test_ref} event = {:call, from} - payload = {:db_call, "test_data"} + payload = {:db_call, self(), "test_data"} {:keep_state, new_data, reply} = Db.handle_event(event, payload, :idle, data) @@ -107,10 +107,10 @@ defmodule Supavisor.DbHandlerTest do end test "handle_event/4 with non-idle state" do - data = %{sock: nil, caller: nil, buffer: []} + data = %{sock: nil, caller: self(), buffer: []} from = {self(), :test_ref} event = {:call, from} - payload = {:db_call, "test_data"} + payload = {:db_call, self(), "test_data"} state = :non_idle {:keep_state, new_data, reply} = Db.handle_event(event, payload, state, data)