Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better handling of resetting active_counter #471

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@

key = {:secrets, tenant_or_alias, user}

case auth_secrets(info, user, key, :timer.hours(24)) do

Check warning on line 278 in lib/supavisor/client_handler.ex

View workflow job for this annotation

GitHub Actions / Code style

Function body is nested too deep (max depth is 2, was 3).
{:ok, auth_secrets} ->
Logger.debug("ClientHandler: Authentication method: #{inspect(auth_secrets)}")

Expand Down Expand Up @@ -334,7 +334,7 @@
Cachex.get(Supavisor.Cache, key) == {:ok, nil} do
case auth_secrets(info, data.user, key, 15_000) do
{:ok, {method2, secrets2}} = value ->
if method != method2 or Map.delete(secrets.(), :client_key) != secrets2.() do

Check warning on line 337 in lib/supavisor/client_handler.ex

View workflow job for this annotation

GitHub Actions / Code style

Function body is nested too deep (max depth is 2, was 4).
Logger.warning("ClientHandler: Update secrets and terminate pool")

Cachex.update(
Expand Down Expand Up @@ -519,21 +519,21 @@
do: :ok = sock_send_maybe_active_once(msg, data),
else: :ok = HandlerHelpers.sock_send(data.sock, Server.ready_for_query())

{:keep_state, %{data | active_count: 0}, handle_actions(data)}
{:keep_state, %{data | active_count: reset_active_count(data)}, handle_actions(data)}
end

def handle_event(:info, {proto, _, <<?S, 4::32, _::binary>> = msg}, _, data)
when proto in @proto do
Logger.debug("ClientHandler: Receive sync while not idle")
:ok = sock_send_maybe_active_once(msg, data)
{:keep_state, %{data | active_count: 0}, handle_actions(data)}
{:keep_state, %{data | active_count: reset_active_count(data)}, handle_actions(data)}
end

def handle_event(:info, {proto, _, <<?H, 4::32, _::binary>> = msg}, _, data)
when proto in @proto do
Logger.debug("ClientHandler: Receive flush while not idle")
:ok = sock_send_maybe_active_once(msg, data)
{:keep_state, %{data | active_count: 0}, handle_actions(data)}
{:keep_state, %{data | active_count: reset_active_count(data)}, handle_actions(data)}
end

# incoming query with a single pool
Expand Down Expand Up @@ -645,17 +645,16 @@
:ready_for_query ->
Logger.debug("ClientHandler: Client is ready")

HandlerHelpers.sock_send(data.sock, bin)
:ok = sock_send_maybe_active_once(bin, data)

db_pid = handle_db_pid(data.mode, data.pool, data.db_pid)

{_, stats} = Telem.network_usage(:client, data.sock, data.id, data.stats)

Telem.client_query_time(data.query_start, data.id)

if data.active_count > @switch_active_count,
do: HandlerHelpers.activate(data.sock)

{:next_state, :idle, %{data | db_pid: db_pid, stats: stats, active_count: 0},
{:next_state, :idle,
%{data | db_pid: db_pid, stats: stats, active_count: reset_active_count(data)},
handle_actions(data)}

:read_sql_error ->
Expand Down Expand Up @@ -1123,4 +1122,14 @@
{:stop, {:shutdown, :subscribe_retries}}
end
end

@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

0
end
end
12 changes: 6 additions & 6 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
tenant = if data.proxy, do: Supavisor.tenant(data.id)
search_path = Supavisor.search_path(data.id)

case send_startup(sock, auth, tenant, search_path) do

Check warning on line 128 in lib/supavisor/db_handler.ex

View workflow job for this annotation

GitHub Actions / Code style

Function body is nested too deep (max depth is 2, was 3).
:ok ->
:ok = activate(sock)
{:next_state, :authentication, %{data | sock: sock}}
Expand Down Expand Up @@ -294,10 +294,10 @@
when is_pid(caller) and proto in @proto do
Logger.debug("DbHandler: Got write replica message #{inspect(bin)}")

if data.active_count > @switch_active_count,
do: HandlerHelpers.active_once(data.sock)

if String.ends_with?(bin, Server.ready_for_query()) do
if data.active_count >= @switch_active_count,
do: HandlerHelpers.activate(data.sock)

{_, stats} = Telem.network_usage(:db, data.sock, data.id, data.stats)

# in transaction mode, we need to notify the client when the transaction is finished,
Expand All @@ -311,11 +311,11 @@
%{data | stats: stats, active_count: 0}
end

if data.active_count > @switch_active_count,
do: HandlerHelpers.activate(data.sock)

{:next_state, :idle, data, {:next_event, :internal, :check_anon_buffer}}
else
if data.active_count > @switch_active_count,
do: HandlerHelpers.active_once(data.sock)

HandlerHelpers.sock_send(data.client_sock, bin)
{:keep_state, %{data | active_count: data.active_count + 1}}
end
Expand Down
Loading