From 64242d7fe912b6436e5546df4906583818d38de0 Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Sat, 14 Oct 2023 16:01:09 -0400 Subject: [PATCH] pr feedback --- lib/db_connection.ex | 9 ++++----- lib/db_connection/connection.ex | 7 ++----- test/test_support.exs | 7 ++----- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/db_connection.ex b/lib/db_connection.ex index ba7fb58..139888a 100644 --- a/lib/db_connection.ex +++ b/lib/db_connection.ex @@ -354,11 +354,10 @@ defmodule DBConnection do processing a request, the last known state will be sent and the exception will be a `DBConnection.ConnectionError`. - If option `disconnect_on_terminate: true` is given to the `connect` callback, - the last known state will be sent and the exception will be a `DBConnection.ConnectionError` - containing the reason for the exit. This callback will be called from `terminate/3` - and it will only happen if the connection was not previously disconnected. - For example, using `disconnect_all/3`. + When the connection is stopped, this callback will be invoked from `terminate`. + The last known state will be sent and the exception will be a `DBConnection.ConnectionError` + containing the reason for the exit. To have the same happen on unexpected + shutdowns, you may trap exits from the `connect` callback. """ @callback disconnect(err :: Exception.t(), state :: any) :: :ok diff --git a/lib/db_connection/connection.ex b/lib/db_connection/connection.ex index 0b1d26e..bc0bbba 100644 --- a/lib/db_connection/connection.ex +++ b/lib/db_connection/connection.ex @@ -76,8 +76,7 @@ defmodule DBConnection.Connection do backoff: Backoff.new(opts), connection_listeners: Keyword.get(opts, :connection_listeners, []), after_connect: Keyword.get(opts, :after_connect), - after_connect_timeout: Keyword.get(opts, :after_connect_timeout, @timeout), - disconnect_on_terminate: opts[:disconnect_on_terminate] + after_connect_timeout: Keyword.get(opts, :after_connect_timeout, @timeout) } {:ok, :no_state, s, {:next_event, :internal, {:connect, :init}}} @@ -358,7 +357,7 @@ defmodule DBConnection.Connection do # and cleanup is not required. def terminate(_, _, %{client: :closed}), do: :ok - def terminate(reason, _, %{disconnect_on_terminate: true} = s) do + def terminate(reason, _, s) do %{mod: mod, state: state} = s msg = "connection exited: " <> Exception.format_exit(reason) @@ -367,8 +366,6 @@ defmodule DBConnection.Connection do |> mod.disconnect(state) end - def terminate(_, _, _), do: :ok - @doc false @impl :gen_statem def format_status(info, [_, :no_state, %{client: :closed, mod: mod}]) do diff --git a/test/test_support.exs b/test/test_support.exs index 5300c2b..ecd4f16 100644 --- a/test/test_support.exs +++ b/test/test_support.exs @@ -2,7 +2,7 @@ defmodule TestConnection do defmacro __using__(opts) do quote do def start_link(opts2) do - defaults = [backoff_type: :exp, backoff_min: 200, disconnect_on_terminate: true] + defaults = [backoff_type: :exp, backoff_min: 200] TestConnection.start_link(opts2 ++ unquote(opts) ++ defaults) end @@ -71,10 +71,7 @@ defmodule TestConnection do def start_link(opts), do: DBConnection.start_link(__MODULE__, opts) def connect(opts) do - if opts[:disconnect_on_terminate] do - Process.flag(:trap_exit, true) - end - + Process.flag(:trap_exit, true) put_agent_from_opts(opts) TestAgent.eval(:connect, [opts]) end