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

feat: shorter retry connect timeout for proxy mode #462

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.4
2.0.5
2 changes: 1 addition & 1 deletion lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@

key = {:secrets, tenant_or_alias, user}

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

Check warning on line 283 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 @@ -339,7 +339,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 342 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 @@ -1120,7 +1120,7 @@
@spec timeout_subscribe_or_terminate(map()) :: :gen_statem.handle_event_result()
def timeout_subscribe_or_terminate(%{subscribe_retries: subscribe_retries} = data) do
if subscribe_retries < @subscribe_retries do
Logger.warning("ClientHandler: Retry subscribe")
Logger.warning("ClientHandler: Retry subscribe #{inspect(subscribe_retries)}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to implement structured logging in Supavisor.


{:keep_state, %{data | subscribe_retries: subscribe_retries + 1},
{:timeout, @timeout_subscribe, :subscribe}}
Expand Down
15 changes: 11 additions & 4 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@type state :: :connect | :authentication | :idle | :busy

@reconnect_timeout 2_500
@reconnect_timeout_proxy 500
@sock_closed [:tcp_closed, :ssl_closed]
@proto [:tcp, :ssl]
@switch_active_count Application.compile_env(:supavisor, :switch_active_count)
Expand Down Expand Up @@ -107,7 +108,7 @@
maybe_reconnect_callback = fn reason ->
if data.reconnect_retries > @reconnect_retries and data.client_sock != nil,
do: {:stop, {:failed_to_connect, reason}},
else: {:keep_state_and_data, {:state_timeout, @reconnect_timeout, :connect}}
else: {:keep_state_and_data, {:state_timeout, reconnect_timeout(data), :connect}}
end

Telem.handler_action(:db_handler, :db_connection, data.id)
Expand All @@ -121,7 +122,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 125 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 @@ -149,8 +150,7 @@
retry = data.reconnect_retries
Logger.warning("DbHandler: Reconnect #{retry} to DB")

{:keep_state, %{data | reconnect_retries: data.reconnect_retries + 1},
{:next_event, :internal, :connect}}
{:keep_state, %{data | reconnect_retries: retry + 1}, {:next_event, :internal, :connect}}
end

def handle_event(:info, {proto, _, bin}, :authentication, data) when proto in @proto do
Expand Down Expand Up @@ -347,7 +347,7 @@
Logger.error("DbHandler: Connection closed when state was #{state}")

if Application.get_env(:supavisor, :reconnect_on_db_close),
do: {:next_state, :connect, data, {:state_timeout, @reconnect_timeout, :connect}},
do: {:next_state, :connect, data, {:state_timeout, reconnect_timeout(data), :connect}},
else: {:stop, :db_termination, data}
end

Expand Down Expand Up @@ -677,4 +677,11 @@
end

defp handle_authentication_error(%{proxy: true}, _reason), do: :ok

@spec reconnect_timeout(map()) :: pos_integer()
def reconnect_timeout(%{proxy: true}),
do: @reconnect_timeout_proxy

def reconnect_timeout(_),
do: @reconnect_timeout
end
Loading