# `Milvex.Connection`

State machine managing gRPC channel lifecycle with automatic reconnection.

Each connection maintains a gRPC channel to a Milvus server and monitors
the underlying connection process for failures.

## States

- `:connecting` - Attempting to establish initial connection
- `:connected` - Channel active, connection monitored
- `:reconnecting` - Lost connection, attempting to restore

## Usage

    # Start a connection
    {:ok, conn} = Milvex.Connection.start_link(host: "localhost", port: 19530)

    # Get the gRPC channel for making calls
    {:ok, channel} = Milvex.Connection.get_channel(conn)

    # Disconnect
    :ok = Milvex.Connection.disconnect(conn)

## Named Connections

    # Start a named connection
    {:ok, _} = Milvex.Connection.start_link([host: "localhost"], name: :milvus)

    # Use the named connection
    {:ok, channel} = Milvex.Connection.get_channel(:milvus)

## Reconnection Behavior

The connection monitors the underlying gRPC connection process. When the
connection dies, it automatically reconnects using exponential backoff
with jitter to prevent thundering herd problems.

Configuration options:
- `:reconnect_base_delay` - Base delay in ms (default: 1000)
- `:reconnect_max_delay` - Maximum delay cap in ms (default: 60000)
- `:reconnect_multiplier` - Exponential multiplier (default: 2.0)
- `:reconnect_jitter` - Jitter factor 0.0-1.0 (default: 0.1)

# `state`

```elixir
@type state() :: :connecting | :connected | :reconnecting
```

# `t`

```elixir
@type t() :: %Milvex.Connection{
  channel: GRPC.Channel.t() | nil,
  config: Milvex.Config.t(),
  conn_monitor_ref: reference() | nil,
  retry_count: non_neg_integer()
}
```

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `connected`

# `connected?`

```elixir
@spec connected?(
  GenServer.server(),
  keyword()
) :: boolean()
```

Checks if the connection is currently established.

# `connecting`

# `disconnect`

```elixir
@spec disconnect(GenServer.server()) :: :ok
```

Disconnects from the Milvus server and stops the connection process.

# `get_channel`

```elixir
@spec get_channel(
  GenServer.server(),
  keyword()
) :: {:ok, GRPC.Channel.t(), Milvex.Config.t()} | {:error, Milvex.Error.t()}
```

Gets the gRPC channel and connection config from the connection.

Returns `{:ok, channel, config}` if connected, or `{:error, error}` if not connected.
The `config` map contains the full `Milvex.Config.t()` used by this connection.

# `reconnecting`

# `start_link`

```elixir
@spec start_link(keyword()) :: GenStateMachine.on_start()
```

Starts a connection to a Milvus server.

## Options

- `:name` - Optional name to register the connection process
- All other options are passed to `Milvex.Config.parse/1`

## Examples

    {:ok, conn} = Milvex.Connection.start_link(host: "localhost", port: 19530)
    {:ok, conn} = Milvex.Connection.start_link([host: "localhost"], name: :milvus)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
