I have a GenServer
that is connecting to a remote TCP connection via gen_tcp
.
opts = [:binary, active: true, packet: :line]
{:ok, socket} = :gen_tcp.connect('remote-url', 8000, opts}
I'm handling messages with:
def handle_info({:tcp, socket, msg}, stage) do
IO.inspect msg
{:noreply, state}
end
Which works great. However, the TCP server is prone to timeouts. If I were using gen_tcp.recv
, I could specify a timeout. However, I am using active: true
to receive messages with handle_info
, and not have to loop over and call recv
. So, the GenServer
happily waits for the next message, even if the server has timed out.
How can I have the GenServer
trigger a function when it hasn't received a message from the TCP connection after X seconds? Am I stuck using recv
?