Skip to content

Commit

Permalink
Refactor insert in connection_status ets table
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Jun 15, 2023
1 parent 16d3bc0 commit 249c714
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/archethic/p2p/client/connection/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ defmodule Archethic.P2P.Client.ConnectionSupervisor do
node_public_key = Keyword.get(opts, :node_public_key)
opts = Keyword.put(opts, :from, self())

:ets.insert(@table_name, {node_public_key, false})

DynamicSupervisor.start_child(
__MODULE__,
%{
Expand All @@ -56,20 +54,23 @@ defmodule Archethic.P2P.Client.ConnectionSupervisor do
"""
@spec node_connected?(node_public_key :: Crypto.key()) :: boolean()
def node_connected?(node_public_key) do
:ets.lookup_element(@table_name, node_public_key, 2)
case :ets.lookup(@table_name, node_public_key) do
[{_key, connected?}] -> connected?
_ -> false
end
end

@doc """
Set node connection status to connected
"""
@spec set_node_connected(node_public_key :: Crypto.key()) :: boolean()
def set_node_connected(node_public_key),
do: :ets.update_element(@table_name, node_public_key, {2, true})
do: :ets.insert(@table_name, {node_public_key, true})

@doc """
Set node connection status to disconnected
"""
@spec set_node_disconnected(node_public_key :: Crypto.key()) :: boolean()
def set_node_disconnected(node_public_key),
do: :ets.update_element(@table_name, node_public_key, {2, false})
do: :ets.insert(@table_name, {node_public_key, false})
end

0 comments on commit 249c714

Please sign in to comment.