# `Juggernaut.Pool`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/pool.ex#L1)

The warm pool: a supervisor of `Juggernaut.Pool.Worker`s, one `:peer` each, all booted
in the sandbox at once (design notes §10.5).

Each worker gets its own environment from the configured `env` templates
(`Juggernaut.Pool.Env.render/3`), so with the default template every worker has its
own `MIX_TEST_PARTITION` and its own test database. `await/2` waits for every boot and
then proves the partitions (`check_partitions/2`): every worker must see exactly the
environment it was given, and no two workers may use the same database. A boot that
fails, or a partition that does not hold, is an infrastructure error (exit 3).

Options of `start_link/1` (validated; a wrong one raises `NimbleOptions.ValidationError`
naming it):

* `:size` (`t:pos_integer/0`) - Required. The number of workers (a run takes it from `options/2`, within the budget).

* `:templates` (list of tuple of `t:String.t/0`, `t:String.t/0` values) - The `env` templates, rendered per worker. The default value is `[{"MIX_TEST_PARTITION", "${MIX_TEST_PARTITION}_jg{worker}"}]`.

* `:controller_env` (map of `t:String.t/0` keys and `t:String.t/0` values) - The controller's environment the templates read (default `System.get_env/0`).

Every other option is passed to each worker (`Juggernaut.Pool.Worker`), which
validates it.

# `await`

```elixir
@spec await(Supervisor.supervisor(), timeout()) ::
  {:ok, %{required(pos_integer()) =&gt; map()}} | {:error, Juggernaut.Error.t()}
```

Waits for every worker's boot and checks the partitions. `{:ok, infos}` gives each
worker's boot info by index; otherwise the first infrastructure error.

# `check_partitions`

```elixir
@spec check_partitions(%{required(pos_integer()) =&gt; map()}, %{
  required(pos_integer()) =&gt; [{String.t(), String.t()}]
}) :: {:ok, map()} | {:error, Juggernaut.Error.t()}
```

`{:ok, infos}` when every worker saw the environment it was given (`expected`, by
index) and no database is used by two workers; otherwise an infrastructure error
naming the workers.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `options`

```elixir
@spec options(Juggernaut.Config.t(), pos_integer(), non_neg_integer()) ::
  {:ok, keyword()} | {:error, Juggernaut.Error.t()}
```

The pool options a run's config gives: `size` within the connection budget
(`Juggernaut.Pool.Env.size/3`, a budget below one worker is a config error; `reserve`
peers' room kept out of it), the `env` templates and each peer's `schedulers`. Merge the
sandbox and worker options in and pass the result to `start_link/1`. With `spares` on,
each worker keeps a parked spare once it has recycled (`Juggernaut.Pool.Worker`,
`spare: :after_recycle`).

# `start_link`

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

Starts the pool and every worker's boot. Use `await/2` before running jobs.

# `workers`

```elixir
@spec workers(Supervisor.supervisor()) :: [{pos_integer(), pid()}]
```

The workers, by index.

---

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