Packages
extreme
1.0.1
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/connection.ex
defmodule Extreme.Connection do
use GenServer
alias Extreme.{Configuration, Tcp, RequestManager}
alias Extreme.ConnectionImpl, as: Impl
require Logger
defmodule State do
defstruct ~w(base_name socket received_data)a
end
def start_link(base_name, configuration),
do: GenServer.start_link(__MODULE__, {base_name, configuration}, name: _name(base_name))
def push(base_name, message) do
:ok =
base_name
|> _name()
|> GenServer.cast({:execute, message})
end
@impl true
def init({base_name, configuration}) do
GenServer.cast(self(), {:connect, configuration, 1})
state = %State{
base_name: base_name,
received_data: ""
}
{:ok, state}
end
@impl true
def handle_cast({:connect, configuration, attempt}, state) do
configuration
|> _connect(attempt)
|> case do
{:ok, socket} ->
Logger.info(fn -> "Successfully connected to EventStore" end)
:ok =
configuration
|> Configuration.get_connection_name()
|> RequestManager.identify_client(state.base_name)
{:noreply, %State{state | socket: socket}}
error ->
{:stop, error, state}
end
end
def handle_cast({:execute, message}, %State{} = state) do
case Impl.execute(message, state) do
:ok -> {:noreply, state}
other -> {:stop, {:execution_error, other}, state}
end
end
@impl true
def handle_info({:tcp, socket, pkg}, %State{socket: socket} = state) do
{:ok, state} = Impl.receive_package(pkg, state)
{:noreply, state}
end
def handle_info({:tcp_closed, _port}, state),
do: {:stop, :tcp_closed, state}
@impl true
def terminate(reason, state) do
Logger.warn("[Extreme] Connection terminated: #{inspect(reason)}")
RequestManager.kill_all_subscriptions(state.base_name)
end
defp _connect(configuration, attempt) do
{:ok, host, port} = Configuration.get_node(configuration)
Tcp.connect(host, port, configuration, attempt)
end
def _name(base_name), do: Module.concat(base_name, Connection)
end