Skip to content

Commit

Permalink
chore: improve active_count (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 authored Nov 14, 2024
1 parent f054c35 commit bfba3fd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
7 changes: 2 additions & 5 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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, _, <<?H, 4::32, _::binary>> = msg}, _, data)
when proto in @proto do
Logger.debug("ClientHandler: Receive flush while not idle")
Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -363,7 +362,7 @@ defmodule Supavisor.DbHandler do
end

if state == :busy or data.mode == :session do
sock_send(data.sock, <<?X, 4::32>>)
sock_send(data.sock, Server.terminate_message())
:gen_tcp.close(elem(data.sock, 1))
{:stop, {:client_handler_down, data.mode}}
else
Expand Down
4 changes: 4 additions & 0 deletions lib/supavisor/protocol/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Supavisor.Protocol.Server do
@scram_request <<?R, 23::32, 10::32, "SCRAM-SHA-256", 0, 0>>
@msg_cancel_header <<16::32, 1234::16, 5678::16>>
@application_name <<?S, 31::32, "application_name", 0, "Supavisor", 0>>
@terminate_message <<?X, 4::32>>

defmodule Pkt do
@moduledoc "Representing a packet structure with tag, length, and payload fields."
Expand Down Expand Up @@ -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

Check warning on line 476 in lib/supavisor/protocol/server.ex

View workflow job for this annotation

GitHub Actions / Code style

Do not use parentheses when defining a function which has no arguments.
end

0 comments on commit bfba3fd

Please sign in to comment.