Skip to content

Commit

Permalink
close the socket when disconnecting, cap at 6 hours for now
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Oct 4, 2024
1 parent 77a511e commit 336ec2a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/archethic/p2p/client/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ defmodule Archethic.P2P.Client.Connection do
) do
# disconnect if missed more than 2 heartbeats
if heartbeats_sent - heartbeats_received >= 2 do
transport.handle_close(socket)
{:next_state, :disconnected, data}
else
transport.handle_send(socket, "hb")
Expand Down Expand Up @@ -645,8 +646,8 @@ defmodule Archethic.P2P.Client.Connection do
@reconnect_delay

:exponential ->
# cap at 24hours
min(:timer.hours(24), 2 ** attempts * @reconnect_delay)
# cap at a few hours
min(:timer.hours(6), 2 ** attempts * @reconnect_delay)
end
end
end
1 change: 1 addition & 0 deletions lib/archethic/p2p/client/transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ defmodule Archethic.P2P.Client.Transport do
@callback handle_connect(:inet.ip_address(), :inet.port_number()) :: {:ok, :inet.socket()}
@callback handle_message(tuple()) :: {:ok, binary()} | {:error, :closed} | {:error, any()}
@callback handle_send(:inet.socket(), binary()) :: :ok
@callback handle_close(:inet.socket()) :: :ok
end
5 changes: 5 additions & 0 deletions lib/archethic/p2p/client/transport/tcp_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ defmodule Archethic.P2P.Client.Transport.TCPImpl do
:gen_tcp.connect(ip, port, @options, 4000)
end

@impl Transport
def handle_close(socket) do
:gen_tcp.close(socket)
end

@impl Transport
def handle_message({:tcp, socket, data}) do
:inet.setopts(socket, active: :once)
Expand Down
32 changes: 32 additions & 0 deletions test/archethic/p2p/client/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ defmodule Archethic.P2P.Client.ConnectionTest do
{:error, :timeout}
end

def handle_close(_socket) do
:ok
end

def handle_send(_socket, <<0::32, _rest::bitstring>>), do: :ok

def handle_message({_, _, _}), do: {:error, :closed}
Expand Down Expand Up @@ -124,6 +128,10 @@ defmodule Archethic.P2P.Client.ConnectionTest do
{:error, :timeout}
end

def handle_close(_socket) do
:ok
end

def handle_send(_socket, <<0::32, _rest::bitstring>>), do: :ok

def handle_message({_, _, _}), do: {:error, :closed}
Expand Down Expand Up @@ -172,6 +180,10 @@ defmodule Archethic.P2P.Client.ConnectionTest do
{:ok, make_ref()}
end

def handle_close(_socket) do
:ok
end

def handle_send(_socket, _), do: :ok

def handle_message({_, _, _}), do: {:error, :closed}
Expand Down Expand Up @@ -274,6 +286,10 @@ defmodule Archethic.P2P.Client.ConnectionTest do
{:ok, make_ref()}
end

def handle_close(_socket) do
:ok
end

def handle_send(_socket, <<0::32, _rest::bitstring>>), do: :ok
def handle_send(_socket, <<_::32, _rest::bitstring>>), do: {:error, :closed}

Expand Down Expand Up @@ -340,6 +356,10 @@ defmodule Archethic.P2P.Client.ConnectionTest do
end
end

def handle_close(_socket) do
:ok
end

def handle_send(_socket, <<0::32, _rest::bitstring>>), do: :ok

def handle_send(_socket, <<_::32, _rest::bitstring>>) do
Expand Down Expand Up @@ -579,6 +599,10 @@ defmodule Archethic.P2P.Client.ConnectionTest do
end
end

def handle_close(_socket) do
:ok
end

def handle_send(_socket, "hb") do
hb_count = :persistent_term.get(:hb_count, 0)
:persistent_term.put(:hb_count, hb_count + 1)
Expand Down Expand Up @@ -621,6 +645,10 @@ defmodule Archethic.P2P.Client.ConnectionTest do
{:ok, make_ref()}
end

def handle_close(_socket) do
:ok
end

def handle_send(_socket, "hb") do
send(self(), {:tcp, make_ref(), "hb"})
:ok
Expand All @@ -640,6 +668,10 @@ defmodule Archethic.P2P.Client.ConnectionTest do
{:ok, make_ref()}
end

def handle_close(_socket) do
:ok
end

def handle_send(_socket, _), do: :ok

def handle_message({_, _, _}), do: {:error, :closed}
Expand Down

0 comments on commit 336ec2a

Please sign in to comment.