Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Rychlewski authored and Greg Rychlewski committed Oct 14, 2023
1 parent 0fbc470 commit 64242d7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
9 changes: 4 additions & 5 deletions lib/db_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions lib/db_connection/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions test/test_support.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 64242d7

Please sign in to comment.