From 1f8fa89584a201e08413b59d80911f3539794942 Mon Sep 17 00:00:00 2001 From: Stas Date: Tue, 12 Nov 2024 17:26:01 +0100 Subject: [PATCH] chore: improve active_count --- lib/supavisor/client_handler.ex | 7 ++----- lib/supavisor/db_handler.ex | 5 ++--- lib/supavisor/protocol/server.ex | 4 ++++ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index 3e8226bf..6ab3ad4e 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -529,6 +529,7 @@ defmodule Supavisor.ClientHandler do {:keep_state, %{data | active_count: reset_active_count(data)}, handle_actions(data)} end + # handle Flush message def handle_event(:info, {proto, _, <> = msg}, _, data) when proto in @proto do Logger.debug("ClientHandler: Receive flush while not idle") @@ -1125,11 +1126,7 @@ defmodule Supavisor.ClientHandler do @spec reset_active_count(map()) :: 0 def reset_active_count(data) do - if data.active_count >= @switch_active_count do - Logger.debug("ClientHandler: Activate socket #{inspect(data.active_count)}") - HandlerHelpers.activate(data.sock) - end - + HandlerHelpers.activate(data.sock) 0 end end diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index 492aadf2..1d7ff475 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -295,8 +295,7 @@ defmodule Supavisor.DbHandler do Logger.debug("DbHandler: Got write replica message #{inspect(bin)}") if String.ends_with?(bin, Server.ready_for_query()) do - if data.active_count >= @switch_active_count, - do: HandlerHelpers.activate(data.sock) + HandlerHelpers.activate(data.sock) {_, stats} = Telem.network_usage(:db, data.sock, data.id, data.stats) @@ -363,7 +362,7 @@ defmodule Supavisor.DbHandler do end if state == :busy or data.mode == :session do - sock_send(data.sock, <>) + sock_send(data.sock, Server.terminate_message()) :gen_tcp.close(elem(data.sock, 1)) {:stop, {:client_handler_down, data.mode}} else diff --git a/lib/supavisor/protocol/server.ex b/lib/supavisor/protocol/server.ex index 02744356..9a865134 100644 --- a/lib/supavisor/protocol/server.ex +++ b/lib/supavisor/protocol/server.ex @@ -15,6 +15,7 @@ defmodule Supavisor.Protocol.Server do @scram_request <> @msg_cancel_header <<16::32, 1234::16, 5678::16>> @application_name <> + @terminate_message <> defmodule Pkt do @moduledoc "Representing a packet structure with tag, length, and payload fields." @@ -470,4 +471,7 @@ defmodule Supavisor.Protocol.Server do @spec application_name() :: binary def application_name, do: @application_name + + @spec terminate_message() :: binary + def terminate_message(), do: @terminate_message end