# `Huginn.Clickhouse.Pool`
[🔗](https://github.com/nyo16/huginn/blob/v0.6.0/lib/huginn/clickhouse/pool.ex#L1)

Supervises one ClickHouse gRPC connection pool.

Starting a pool registers its `Huginn.Clickhouse.Config` under the config's
`:pool_name`, so every query routed to that pool (via the `:pool` option)
uses that pool's database and credentials, then starts the underlying
`GrpcConnectionPool`. The usual entry point is `Huginn.child_spec/1`, which
delegates here.

## Single pool

Add the pool to your application's supervision tree:

    children = [
      {Huginn, host: "localhost", port: 9100, auth: {:password, "default", ""}}
    ]

Alternatively set `config :huginn, :clickhouse, host: ...` and Huginn's own
application starts the pool for you.

## Multiple pools

Pools are identified by `:pool_name`, so any number can coexist under one
supervisor — for example against different hosts or with different
credentials:

    children = [
      {Huginn,
       pool_name: :analytics,
       host: "analytics.internal",
       database: "analytics",
       auth: {:password, "reader", System.fetch_env!("ANALYTICS_PASSWORD")}},
      {Huginn,
       pool_name: :events,
       host: "events.internal",
       database: "events",
       auth: {:jwt, System.fetch_env!("EVENTS_JWT")},
       ssl: true}
    ]

    Huginn.query("SELECT count() FROM visits", pool: :analytics)
    Huginn.query("SELECT count() FROM clicks", pool: :events)

Starting a second pool under a name that is already running fails with
`{:error, {:already_started, pid}}` and leaves the running pool's config
untouched.

A pool starts even when the server is unreachable; its workers keep
reconnecting in the background. Use `await_ready/2` to block until at least
one connection is up.

# `await_ready`

```elixir
@spec await_ready(atom(), pos_integer()) :: :ok | {:error, Huginn.Error.t()}
```

Blocks until the pool has at least one connected channel.

Returns `{:error, %Huginn.Error{reason: :not_configured}}` when no pool is
registered under `pool_name`, and `{:error, %Huginn.Error{reason: :connection}}`
when no connection came up within `timeout` milliseconds.

# `child_spec`

```elixir
@spec child_spec(keyword() | map() | Huginn.Clickhouse.Config.t()) ::
  Supervisor.child_spec()
```

Returns a supervisor child spec for a pool.

Accepts the options of `Huginn.Clickhouse.Config.new/1` (keyword list or map)
or a `Huginn.Clickhouse.Config` struct. The child id is
`{Huginn.Clickhouse.Pool, pool_name}`, so pools with different names can be
started under the same supervisor.

# `start_link`

```elixir
@spec start_link(keyword() | map() | Huginn.Clickhouse.Config.t()) ::
  Supervisor.on_start()
```

Starts a pool supervisor. See `child_spec/1` for the accepted options.

---

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