Skip to content

Commit

Permalink
update tcp_connection example
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Nov 11, 2024
1 parent 00c6958 commit ba7677a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test,integration_test}/**/*.{ex,exs}"]
inputs: ["{mix,.formatter}.exs", "{config,lib,test,examples,integration_test}/**/*.{ex,exs}"]
]
9 changes: 9 additions & 0 deletions examples/tcp_connection/lib/tcp_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ 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 @@ -143,6 +145,13 @@ 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} = s) do
{:disconnect, TCPConnection.Error.exception({:idle, :closed}), s}
end

@impl true
def handle_close(_, _, s) do
{:ok, nil, s}
Expand Down

0 comments on commit ba7677a

Please sign in to comment.