From 71a8f4c7f3d018b3584e6eee1ea922c65f07db0e Mon Sep 17 00:00:00 2001 From: Stas Date: Tue, 23 Jan 2024 13:38:35 +0100 Subject: [PATCH 1/4] fix: kill the linked handler in session mode --- lib/supavisor/db_handler.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index 7c1156eb..04cdd5c8 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -384,7 +384,7 @@ defmodule Supavisor.DbHandler do Logger.error("Client handler #{inspect(pid)} went down with reason #{inspect(reason)}") end - if state == :busy do + if state == :busy || data.mode == :session do :ok = sock_send(data.sock, <>) :ok = :gen_tcp.close(elem(data.sock, 1)) {:stop, :normal, data} From 2b5dd3641adbfb08da5492f4c1f902203ca0fe61 Mon Sep 17 00:00:00 2001 From: Stas Date: Tue, 23 Jan 2024 13:40:01 +0100 Subject: [PATCH 2/4] bump version --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 852ed67c..4e036596 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.18 +1.1.19 From f18652fe2c0463cd693a20fb02f3c8dd2a65b7a2 Mon Sep 17 00:00:00 2001 From: Stas Date: Tue, 23 Jan 2024 18:23:51 +0100 Subject: [PATCH 3/4] feat: add more logs (#278) --- VERSION | 2 +- lib/supavisor/client_handler.ex | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/VERSION b/VERSION index 4e036596..be5b4c7b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.19 +1.1.20 diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index c7851b04..f0a91183 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -526,12 +526,21 @@ defmodule Supavisor.ClientHandler do :ok end - def terminate(_reason, _state, %{db_pid: {_, pid}}) do - if Db.get_state(pid) == :busy, do: Db.stop(pid) + def terminate(reason, _state, %{db_pid: {_, pid}}) do + if Db.get_state(pid) == :busy do + Logger.warning("Kill DbHandler #{inspect(pid)}, reason #{inspect(reason)}") + Db.stop(pid) + else + Logger.warning("ClientHanlder termination with reason #{inspect(reason)}") + end + :ok end - def terminate(_reason, _state, _data), do: :ok + def terminate(reason, _state, _data) do + Logger.warning("ClientHanlder termination with reason #{inspect(reason)}") + :ok + end ## Internal functions From 31be38f6f6c3aea58a5bd487e3b688857aa6a961 Mon Sep 17 00:00:00 2001 From: Stas Date: Wed, 24 Jan 2024 09:43:01 +0100 Subject: [PATCH 4/4] chore: set max heap size for handlers (#280) --- VERSION | 2 +- lib/supavisor/client_handler.ex | 1 + lib/supavisor/db_handler.ex | 2 ++ lib/supavisor/helpers.ex | 13 +++++++++++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index be5b4c7b..6f182425 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.20 +1.1.21 diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index f0a91183..d2d3977d 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -35,6 +35,7 @@ defmodule Supavisor.ClientHandler do def init(ref, trans, opts) do Process.flag(:trap_exit, true) + H.set_max_heap_size(150) {:ok, sock} = :ranch.handshake(ref) :ok = trans.setopts(sock, active: true) diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index 04cdd5c8..eee3e45b 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -35,6 +35,8 @@ defmodule Supavisor.DbHandler do @impl true def init(args) do Process.flag(:trap_exit, true) + H.set_max_heap_size(150) + {_, tenant} = args.tenant Logger.metadata(project: tenant, user: args.user, mode: args.mode) diff --git a/lib/supavisor/helpers.ex b/lib/supavisor/helpers.ex index 8cc295c2..d23108ae 100644 --- a/lib/supavisor/helpers.ex +++ b/lib/supavisor/helpers.ex @@ -330,4 +330,17 @@ defmodule Supavisor.Helpers do kind, reason -> {:error, {:badrpc, {kind, reason}}} end end + + @doc """ + Sets the maximum heap size for the current process. The `max_heap_size` parameter is in megabytes. + + ## Parameters + + - `max_heap_size`: The maximum heap size in megabytes. + """ + @spec set_max_heap_size(pos_integer()) :: map() + def set_max_heap_size(max_heap_size) do + max_heap_words = div(max_heap_size * 1024 * 1024, :erlang.system_info(:wordsize)) + Process.flag(:max_heap_size, %{size: max_heap_words}) + end end