# `ExMqtt.State.Inflight`
[🔗](https://github.com/Ultraluv/ex_mqtt_uv/blob/v0.1.0/lib/ex_mqtt/state/inflight.ex#L1)

Pure functional QoS 1/2 publish state machine and packet identifier manager,
supporting strict QoS state transitions, dual-limit flow control
(`min(max_inflight, server_receive_maximum)`), and periodic retry scanning.

## Outgoing QoS state transitions
- QoS 1: `:puback_pending` --(recv PUBACK)--> completed & removed
- QoS 2: `:pubrec_pending` --(recv PUBREC)--> `:pubcomp_pending` --(recv PUBCOMP)--> completed & removed

## Incoming QoS 2 state transitions
- recv PUBLISH (QoS 2) --> `:pubrel_pending` (send PUBREC, deliver message once)
- recv PUBREL --> completed & removed (send PUBCOMP)

# `ack_target`

```elixir
@type ack_target() ::
  {:sync, GenServer.from()} | {:async, pid(), (term() -&gt; any()) | nil} | nil
```

# `control_entry`

```elixir
@type control_entry() :: %{
  type: :subscribe | :unsubscribe,
  from: GenServer.from(),
  payload: term()
}
```

# `outgoing_entry`

```elixir
@type outgoing_entry() :: %{
  packet_id: 1..65535,
  qos: 1 | 2,
  state: outgoing_state(),
  packet:
    ExMqtt.Protocol.Packet.Publish.t() | ExMqtt.Protocol.Packet.Pubrel.t(),
  ack_target: ack_target(),
  inserted_at: integer(),
  seq: non_neg_integer()
}
```

# `outgoing_state`

```elixir
@type outgoing_state() :: :puback_pending | :pubrec_pending | :pubcomp_pending
```

# `t`

```elixir
@type t() :: %ExMqtt.State.Inflight{
  incoming_qos2: %{optional(1..65535) =&gt; :pubrel_pending},
  max_inflight: pos_integer(),
  next_packet_id: 1..65535,
  next_seq: non_neg_integer(),
  outgoing: %{optional(1..65535) =&gt; outgoing_entry()},
  pending_control: %{optional(1..65535) =&gt; control_entry()},
  receive_maximum: pos_integer()
}
```

# `ack_puback`

```elixir
@spec ack_puback(t(), 1..65535) ::
  {:ok, outgoing_entry(), t()} | {:error, :not_found | :illegal_state}
```

Processes an incoming PUBACK for an outgoing QoS 1 publish.

# `ack_pubcomp`

```elixir
@spec ack_pubcomp(t(), 1..65535) ::
  {:ok, outgoing_entry(), t()} | {:error, :not_found | :illegal_state}
```

Processes an incoming PUBCOMP for an outgoing QoS 2 publish.

# `ack_pubrec`

```elixir
@spec ack_pubrec(t(), 1..65535, boolean()) ::
  {:ok, :send_pubrel | :aborted, outgoing_entry(), t()}
  | {:error, :not_found | :illegal_state}
```

Processes an incoming PUBREC for an outgoing QoS 2 publish.

# `allocate_packet_id`

```elixir
@spec allocate_packet_id(t()) ::
  {:ok, 1..65535, t()} | {:error, :no_packet_id_available}
```

Allocates the next unused Packet Identifier (`1..65_535`).

# `clear_session`

```elixir
@spec clear_session(t()) :: {[GenServer.from()], t()}
```

Clears all session state (used when broker responds with `session_present: false`),
returning all pending sync `from` references so callers can be notified.

# `complete_incoming_qos2`

```elixir
@spec complete_incoming_qos2(t(), 1..65535) :: {:ok | :not_found, t()}
```

Completes an incoming QoS 2 PUBLISH upon receiving PUBREL.

# `effective_limit`

```elixir
@spec effective_limit(t()) :: pos_integer()
```

Returns the effective inflight window `min(max_inflight, receive_maximum)`.

# `full?`

```elixir
@spec full?(t()) :: boolean()
```

Returns true when outgoing inflight count has reached `effective_limit/1`.

# `new`

```elixir
@spec new(keyword()) :: t()
```

Creates a new inflight state.

## Options
- `:max_inflight` - Client-side maximum concurrent QoS 1/2 publishes (default `65_535`)
- `:receive_maximum` - Server-advertised Receive Maximum (default `65_535`)

# `outgoing_count`

```elixir
@spec outgoing_count(t()) :: non_neg_integer()
```

Returns the number of active outgoing QoS 1/2 publishes.

# `packets_to_resend`

```elixir
@spec packets_to_resend(t()) :: [
  ExMqtt.Protocol.Packet.Publish.t() | ExMqtt.Protocol.Packet.Pubrel.t()
]
```

Returns all unacknowledged outgoing packets ordered by original sequence number
for retransmission upon session resumption (`session_present: true`).

# `pop_control`

```elixir
@spec pop_control(t(), 1..65535, :subscribe | :unsubscribe) ::
  {:ok, control_entry(), t()} | :error
```

Pops a pending SUBSCRIBE or UNSUBSCRIBE request.

# `set_receive_maximum`

```elixir
@spec set_receive_maximum(t(), pos_integer()) :: t()
```

Updates the negotiated server `receive_maximum` (MQTT 5.0 CONNACK).

# `take_expired_for_retry`

```elixir
@spec take_expired_for_retry(t(), pos_integer(), integer()) ::
  {[ExMqtt.Protocol.Packet.Publish.t() | ExMqtt.Protocol.Packet.Pubrel.t()],
   t()}
```

Returns unacknowledged outgoing packets older than `retry_interval_ms`, ordered
by sequence number, and refreshes their `inserted_at` timestamps.

# `track_control`

```elixir
@spec track_control(
  t(),
  1..65535,
  :subscribe | :unsubscribe,
  GenServer.from(),
  term()
) :: t()
```

Tracks a pending SUBSCRIBE or UNSUBSCRIBE request with its payload.

# `track_incoming_qos2`

```elixir
@spec track_incoming_qos2(t(), 1..65535) :: {:new | :duplicate, t()}
```

Records an incoming QoS 2 PUBLISH.

# `track_outgoing_publish`

```elixir
@spec track_outgoing_publish(t(), ExMqtt.Protocol.Packet.Publish.t(), ack_target()) ::
  {:ok, t()} | {:error, :receive_maximum_exceeded | :packet_id_in_use}
```

Tracks a newly sent outgoing QoS 1 or QoS 2 PUBLISH packet.

---

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