Skip to content

Commit

Permalink
remove the max_connection_attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Dec 12, 2023
1 parent a8faf44 commit 784e418
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 79 deletions.
46 changes: 6 additions & 40 deletions lib/archethic/p2p/client/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Archethic.P2P.Client.Connection do
use GenStateMachine, callback_mode: [:handle_event_function, :state_enter], restart: :temporary
@vsn Mix.Project.config()[:version]
@table_name :connection_status
@max_reconnect_attempts 5

@heartbeat_interval Keyword.get(
Application.compile_env(:archethic, __MODULE__, []),
:heartbeat_interval,
Expand Down Expand Up @@ -70,18 +70,6 @@ defmodule Archethic.P2P.Client.Connection do
end
end

@doc """
When called, if disconnect, it will try to connect to socket
Noop if it's already connected
It's used when some node has been offline for a long time
It has connected to us so we know we can connect to it as well
"""
@spec wake_up(Crypto.key()) :: :ok
def wake_up(public_key) do
GenStateMachine.cast(via_tuple(public_key), :wake_up)
end

@doc """
Get the availability timer and reset it with a new start time if it was already started
"""
Expand Down Expand Up @@ -290,14 +278,10 @@ defmodule Archethic.P2P.Client.Connection do

# this message is used to delay next connection attempt
def handle_event({:timeout, :reconnect}, _event_data, _state, data) do
if data.reconnect_attempts >= @max_reconnect_attempts do
:keep_state_and_data
else
actions = [{:next_event, :internal, {:connect, nil}}]
actions = [{:next_event, :internal, {:connect, nil}}]

new_data = Map.update!(data, :reconnect_attempts, &(&1 + 1))
{:keep_state, new_data, actions}
end
new_data = Map.update!(data, :reconnect_attempts, &(&1 + 1))
{:keep_state, new_data, actions}
end

def handle_event(
Expand All @@ -310,25 +294,6 @@ defmodule Archethic.P2P.Client.Connection do
:keep_state_and_data
end

def handle_event(
:cast,
:wake_up,
:disconnected,
data
) do
actions = [{:next_event, :internal, {:connect, nil}}]
{:keep_state, %{data | reconnect_attempts: 0}, actions}
end

def handle_event(
:cast,
:wake_up,
_,
_data
) do
:keep_state_and_data
end

def handle_event(
:cast,
{:send_message, ref, from, message, timeout},
Expand Down Expand Up @@ -638,7 +603,8 @@ defmodule Archethic.P2P.Client.Connection do
@reconnect_delay

:exponential ->
2 ** attempts * @reconnect_delay
# cap at 24hours
min(:timer.hours(24), 2 ** attempts * @reconnect_delay)
end
end
end
4 changes: 0 additions & 4 deletions lib/archethic/p2p/listener_protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule Archethic.P2P.ListenerProtocol do

alias Archethic.Crypto

alias Archethic.P2P.Client.Connection
alias Archethic.P2P.Message
alias Archethic.P2P.MessageEnvelop

Expand Down Expand Up @@ -84,9 +83,6 @@ defmodule Archethic.P2P.ListenerProtocol do
start_processing_time = System.monotonic_time()
response = Message.process(message, sender_public_key)

# we may attempt to wakeup a connection that offline
Connection.wake_up(sender_public_key)

:telemetry.execute(
[:archethic, :p2p, :handle_message],
%{
Expand Down
35 changes: 0 additions & 35 deletions test/archethic/p2p/client/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ defmodule Archethic.P2P.Client.ConnectionTest do
:heartbeat_interval,
10_000
)
@reconnect_delay Keyword.get(
Application.compile_env(:archethic, Connection, []),
:reconnect_delay,
500
)

test "start_link/1 should open a socket and a connection worker and initialize the backlog and lookup tables" do
{:ok, pid} =
Expand Down Expand Up @@ -198,36 +193,6 @@ defmodule Archethic.P2P.Client.ConnectionTest do
assert {{:connected, _socket}, _} = :sys.get_state(pid)
end

test "should stop trying to reconnect after some time" do
defmodule MockTransportOffline do
alias Archethic.P2P.Client.Transport

@behaviour Transport

def handle_connect({127, 0, 0, 1}, _port) do
{:error, :timeout}
end

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

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

{:ok, pid} =
Connection.start_link(
transport: MockTransportOffline,
ip: {127, 0, 0, 1},
port: 3000,
node_public_key: Crypto.first_node_public_key()
)

Process.sleep(@reconnect_delay * 6)
assert {:disconnected, %{reconnect_attempts: 5}} = :sys.get_state(pid)

Process.sleep(@reconnect_delay * 2)
assert {:disconnected, %{reconnect_attempts: 5}} = :sys.get_state(pid)
end

test "should get an error when the timeout is reached" do
{:ok, pid} =
Connection.start_link(
Expand Down

0 comments on commit 784e418

Please sign in to comment.