Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a6f2020

Browse files
authoredJan 22, 2025··
Call disconnect on protocol when reconnecting in Replication connection (#726)
1 parent 9748fcb commit a6f2020

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
 

‎lib/postgrex/replication_connection.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,22 @@ defmodule Postgrex.ReplicationConnection do
490490
{:keep_state, s, {:next_event, :internal, {:connect, :backoff}}}
491491
end
492492

493+
def handle_event(:internal, {:connect, :reconnect}, @state, %{protocol: protocol} = state)
494+
when protocol != nil do
495+
Protocol.disconnect(:reconnect, protocol)
496+
{:keep_state, %{state | protocol: nil}, {:next_event, :internal, {:connect, :init}}}
497+
end
498+
493499
def handle_event(:internal, {:connect, _info}, @state, %{state: {mod, mod_state}} = s) do
494500
case Protocol.connect(opts()) do
495501
{:ok, protocol} ->
496502
maybe_handle(mod, :handle_connect, [mod_state], %{s | protocol: protocol})
497503

498504
{:error, reason} ->
505+
Logger.error(
506+
"#{inspect(pid_or_name())} (#{inspect(mod)}) failed to connect to Postgres: #{Exception.format(:error, reason)}"
507+
)
508+
499509
if s.auto_reconnect do
500510
{:keep_state, s, {{:timeout, :backoff}, s.reconnect_backoff, nil}}
501511
else

‎lib/postgrex/simple_connection.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ defmodule Postgrex.SimpleConnection do
362362
end
363363

364364
{:error, reason} ->
365+
Logger.error(
366+
"#{inspect(pid_or_name())} (#{inspect(mod)}) failed to connect to Postgres: #{Exception.format(:error, reason)}"
367+
)
368+
365369
if state.auto_reconnect do
366370
{:keep_state, state, {{:timeout, :backoff}, state.reconnect_backoff, nil}}
367371
else
@@ -466,6 +470,13 @@ defmodule Postgrex.SimpleConnection do
466470
end
467471
end
468472

473+
defp pid_or_name do
474+
case Process.info(self(), :registered_name) do
475+
{:registered_name, atom} when is_atom(atom) -> atom
476+
_ -> self()
477+
end
478+
end
479+
469480
defp opts(mod), do: Process.get(mod)
470481

471482
defp put_opts(mod, opts), do: Process.put(mod, opts)

0 commit comments

Comments
 (0)
Please sign in to comment.