Packages
extreme
1.0.0
1.1.4
1.1.3
1.1.2
1.1.1
1.1.1-rc01
1.1.0
1.1.0-rc9
1.1.0-rc8
1.1.0-rc7
1.1.0-rc6
1.1.0-rc5
1.1.0-rc4
1.1.0-rc3
1.1.0-rc2
1.1.0-rc1
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.13.4
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.2
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
Elixir TCP client for EventStore.
Current section
Files
Jump to
Current section
Files
lib/extreme/tcp.ex
defmodule Extreme.Tcp do
@moduledoc """
Set of functions for TCP communication.
"""
require Logger
@doc """
Opens tcp connection with EventStore. Returns `{:ok, socket}` on success or
`{:error, :max_attempt_exceeded}` if connection wasn't made in `:max_attempts`
provided in `configuration`. If not specified, `max_attempts` defaults to :infinity
"""
def connect(host, port, configuration, attempt \\ 1) do
configuration
|> Keyword.get(:max_attempts, :infinity)
|> case do
:infinity -> true
max when attempt <= max -> true
_any -> false
end
|> if do
if attempt > 1 do
configuration
|> Keyword.get(:reconnect_delay, 1_000)
|> :timer.sleep()
end
_connect(host, port, configuration, attempt)
else
{:error, :max_attempt_exceeded}
end
end
defp _connect(host, port, configuration, attempt) do
Logger.info(fn -> "Connecting Extreme to #{host}:#{port}" end)
opts = [:binary, active: :once]
host
|> :gen_tcp.connect(port, opts)
|> case do
{:ok, socket} ->
{:ok, socket}
reason ->
Logger.warn(fn -> "Error connecting to EventStore: #{inspect(reason)}" end)
{:error, reason}
connect(host, port, configuration, attempt + 1)
end
end
end