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

Proxy info messages to the adapter #316

Merged
merged 8 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions examples/tcp_connection/lib/tcp_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,16 @@ defmodule TCPConnection do
# 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} = s) do
{:disconnect, TCPConnection.Error.exception({:idle, :closed}), s}
def handle_info({:DOWN, _ref, _type, sock, _info}, {sock, _buffer}) do
{:disconnect, TCPConnection.Error.exception({:idle, :closed})}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like here.

end

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

:ok
end

@impl true
Expand Down
59 changes: 57 additions & 2 deletions integration_test/cases/info_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
defmodule TestInfo do
defmodule InfoTest do
use ExUnit.Case, async: true
alias TestPool, as: P
alias TestAgent, as: A

test "handle_info returns new state"
test "handle_info handles message and moves on" do
stack = [
fn opts ->
send(opts[:parent], {:connected, self()})
{:ok, :state}
end,
:ok,
{:idle, :state},
{:idle, :state}
]

{:ok, agent} = A.start_link(stack)
{:ok, pool} = P.start_link(agent: agent, parent: self())

assert_receive {:connected, conn}
send(conn, "some harmless message")
assert P.run(pool, fn _ -> :result end) == :result
Copy link
Contributor Author

@ruslandoga ruslandoga Nov 11, 2024

Choose a reason for hiding this comment

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

I'm using this just to "sync" with the connection process, i.e. wait until it has processed all the messages. Maybe there is a better way? I know some people use :sys.get_state/1 (e.g. :sys.get_state(conn) in this case) but don't know if it's any better.


assert [
connect: _,
handle_info: _,
handle_status: _,
handle_status: _
] = A.record(agent)
end

test "handle_info can force disconnect" do
ruslandoga marked this conversation as resolved.
Show resolved Hide resolved
stack = [
fn opts ->
send(opts[:parent], {:connected, self()})
{:ok, :state}
end,
{:disconnect, :reason},
:ok,
fn opts ->
send(opts[:parent], :reconnected)
{:ok, :state}
end
]

{:ok, agent} = A.start_link(stack)
P.start_link(agent: agent, parent: self())

assert_receive {:connected, conn}
send(conn, "some harmful message")
assert_receive :reconnected

assert [
connect: _,
handle_info: _,
disconnect: _,
connect: _
] = A.record(agent)
end
end
2 changes: 0 additions & 2 deletions integration_test/connection_pool/disconnect_all_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,4 @@ defmodule TestPoolDisconnectAll do
handle_execute: [_, _, _, :final_state1]
] = A.record(agent)
end

test "disconnect on info"
end
8 changes: 4 additions & 4 deletions lib/db_connection/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,11 @@ defmodule DBConnection.Connection do
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
Copy link
Contributor Author

@ruslandoga ruslandoga Nov 11, 2024

Choose a reason for hiding this comment

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

I'm still passing the state to the callback since the monitor ref / socket ref would be there, and they are probably needed to match on the incoming message.

{:ok, state} ->
pool_update(state, s)
:ok ->
handle_timeout(s)

{:disconnect, err, state} ->
{:keep_state, %{s | state: state}, {:next_event, :internal, {:disconnect, {:log, err}}}}
{:disconnect, err} ->
{:keep_state, s, {:next_event, :internal, {:disconnect, {:log, err}}}}
end
else
Logger.info(fn ->
Expand Down
4 changes: 4 additions & 0 deletions test/test_support.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ 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