# `Alternera.Plugins.Camel.Inbox`
[🔗](https://github.com/10e-ab/alternera-camel/blob/v0.9.0/alternera_plugin_camel/lib/alternera/plugins/camel/inbox.ex#L1)

Where a pushing Camel source's messages wait on the proxy until the
trigger's runner takes them (`Plugins.Camel.Source`, delivery `push`).

The host sends each delivery to the sink its open named, in a `messages`
frame (`PluginHosts.Host.subscribe/3`), while fewer than the open's
prefetch are held; the inbox queues them, and a receive takes up to its
`max`, waiting up to its wait for the first (`take/3`). So nothing polls
the host: an idle source costs one frame every `@beat_ms`.

A pushed delivery is held on the host for `hold_ms/0` from its push.
Every `@beat_ms` the inbox extends what is still queued by that hold
again, in one `extend` frame, which also tells the host the session is in
use (an empty one included): without it an idle session closes after the
host's `SESSION_IDLE_MS`. A handle the extend answers `gone` lapsed and
leaves the queue. Once taken, a message's hold is the runner's.

The session is lost when the host says so (`"closed"`, or `host_down`
when its process ended), when an extend answers `unknown_session`, or
when the Host process goes: a take answers `session_lost` and the runner
opens the source again. An inbox whose session nobody used for
`@idle_ms` (no take, and no settle or extend through `touch/1`: a runner
whose every slot runs a long message takes nothing, but extends their
holds) belongs to a runner that went away without closing it: it closes
the session and stops.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `hold_ms`

How long the host holds a pushed delivery before the inbox extends it.

# `opened`

```elixir
@spec opened(pid(), String.t()) :: :ok
```

The session the open answered: the inbox starts holding its messages for it.

# `start`

```elixir
@spec start(pid(), String.t(), String.t(), :json | :msgpack, keyword()) ::
  {:ok, pid()} | {:error, term()}
```

Starts an inbox for `source` on the Host `host` under the sink `sink`,
subscribed before the open is sent, so no message finds no sink. Its
beat starts at once, so an inbox whose open never answered (its opener
crashed between the two) still closes as idle. `opts` are for tests:
`:beat_ms` and `:idle_ms`.

# `start_link`

# `stop`

```elixir
@spec stop(pid()) :: :ok
```

Stops the inbox, its sink dropped; what it still queued lapses back to the host.

# `take`

```elixir
@spec take(pid(), pos_integer(), non_neg_integer()) ::
  {:ok, [map()]} | {:error, String.t(), String.t()}
```

Up to `max` messages as the host sent them, waiting up to `wait_ms` for
the first: `{:ok, messages}` (`[]` when none came) or `{:error,
"session_lost", message}`.

# `touch`

```elixir
@spec touch(pid()) :: :ok
```

The session is in use: a settle or an extend of what the runner holds.

---

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