diff options
Diffstat (limited to 'lib/manager.ex')
-rw-r--r-- | lib/manager.ex | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/manager.ex b/lib/manager.ex index 45aae5f..87f95c5 100644 --- a/lib/manager.ex +++ b/lib/manager.ex @@ -28,6 +28,8 @@ defmodule Shard.Manager do use GenServer + require Logger + def start_link(my_port) do GenServer.start_link(__MODULE__, my_port, name: __MODULE__) end @@ -96,7 +98,9 @@ defmodule Shard.Manager do add_peer(ip, port, state) currtime = System.os_time :second :ets.insert(state.outbox, {peer_id, msg, currtime}) - outbox_cleanup = :ets.fun2ms(fn {_, _, t} when t < currtime - 60 -> true end) + outbox_cleanup = [{{:_, :_, :'$1'}, + [{:<, :'$1', currtime - 60}], + [:'$1']}] :ets.select_delete(state.outbox, outbox_cleanup) _ -> nil end @@ -121,9 +125,13 @@ defmodule Shard.Manager do defp add_peer(ip, port, state) do spawn fn -> - {:ok, client} = :gen_tcp.connect(ip, port, [:binary, packet: 2, active: false]) - {:ok, pid} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SNet.TCPConn, %{socket: client, my_port: state.my_port}}) - :ok = :gen_tcp.controlling_process(client, pid) + case :gen_tcp.connect(ip, port, [:binary, packet: 2, active: false]) do + {:ok, client} -> + {:ok, pid} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SNet.TCPConn, %{socket: client, my_port: state.my_port}}) + :ok = :gen_tcp.controlling_process(client, pid) + _ -> + Logger.info "Could not connect to #{inspect ip}:#{port}, some messages may be dropped" + end end end |