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

Revert handle_info/2 callback #319

Merged
merged 3 commits into from
Dec 19, 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
17 changes: 0 additions & 17 deletions examples/tcp_connection/lib/tcp_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ defmodule TCPConnection do

case :gen_tcp.connect(host, port, socket_opts, timeout) do
{:ok, sock} ->
# Monitor the socket so we can react to it being closed. See handle_info/2.
_ref = :inet.monitor(sock)
{:ok, {sock, <<>>}}

{:error, reason} ->
Expand Down Expand Up @@ -145,21 +143,6 @@ defmodule TCPConnection do
end
end

# The handle_info callback is optional and can be removed if not needed.
# Here it is used to react to `:inet.monitor/1` messages which arrive
# when socket gets closed while the connection is idle.
def handle_info({:DOWN, _ref, _type, sock, _info}, {sock, _buffer}) do
{:disconnect, TCPConnection.Error.exception({:idle, :closed})}
end

def handle_info(msg, state) do
Logger.info(fn ->
["#{__MODULE__} (", inspect(self()), ") missed message: ", inspect(msg)]
end)

:ok
end

@impl true
def handle_close(_, _, s) do
{:ok, nil, s}
Expand Down
100 changes: 0 additions & 100 deletions integration_test/cases/info_test.exs

This file was deleted.

9 changes: 0 additions & 9 deletions lib/db_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,6 @@ defmodule DBConnection do
"""
@callback disconnect(err :: Exception.t(), state :: any) :: :ok

@doc """
Optional callback for handling messages received by the connection.

Returns `{:disconnect, exception}` to close the connection or `:ok` to continue.
"""
@callback handle_info(message :: any, state :: any) :: {:disconnect, Exception.t()} | :ok

@optional_callbacks handle_info: 2

@connection_module_key :connection_module

@doc """
Expand Down
20 changes: 5 additions & 15 deletions lib/db_connection/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -330,22 +330,12 @@ defmodule DBConnection.Connection do
handle_timeout(s)
end

def handle_event(:info, msg, :no_state, %{mod: mod, state: state} = s) do
if function_exported?(mod, :handle_info, 2) do
case apply(mod, :handle_info, [msg, state]) do
:ok ->
handle_timeout(s)

{:disconnect, err} ->
{:keep_state, s, {:next_event, :internal, {:disconnect, {:log, err}}}}
end
else
Logger.info(fn ->
[inspect(mod), ?\s, ?(, inspect(self()), ") missed message: " | inspect(msg)]
end)
def handle_event(:info, msg, :no_state, %{mod: mod} = s) do
Logger.info(fn ->
[inspect(mod), ?\s, ?(, inspect(self()), ") missed message: " | inspect(msg)]
end)

handle_timeout(s)
end
handle_timeout(s)
end

@doc false
Expand Down
4 changes: 0 additions & 4 deletions test/test_support.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ defmodule TestConnection do
TestAgent.eval(:handle_deallocate, [query, cursor, opts, state])
end

def handle_info(message, state) do
TestAgent.eval(:handle_info, [message, state])
end

defp put_agent_from_opts(opts) do
Process.get(:agent) || Process.put(:agent, agent_from_opts(opts))
end
Expand Down
Loading