ExMqtt.State.Inflight (ex_mqtt_uv v0.1.0)

Copy Markdown View Source

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)

Summary

Functions

Processes an incoming PUBACK for an outgoing QoS 1 publish.

Processes an incoming PUBCOMP for an outgoing QoS 2 publish.

Processes an incoming PUBREC for an outgoing QoS 2 publish.

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

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

Completes an incoming QoS 2 PUBLISH upon receiving PUBREL.

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

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

Creates a new inflight state.

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

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

Pops a pending SUBSCRIBE or UNSUBSCRIBE request.

Updates the negotiated server receive_maximum (MQTT 5.0 CONNACK).

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

Tracks a pending SUBSCRIBE or UNSUBSCRIBE request with its payload.

Records an incoming QoS 2 PUBLISH.

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

Types

ack_target()

@type ack_target() ::
  {:sync, GenServer.from()} | {:async, pid(), (term() -> any()) | nil} | nil

control_entry()

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

outgoing_entry()

@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()

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

t()

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

Functions

ack_puback(state, pid)

@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(state, pid)

@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(state, pid, error? \\ false)

@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(state)

@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(state)

@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(state, pid)

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

Completes an incoming QoS 2 PUBLISH upon receiving PUBREL.

effective_limit(inflight)

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

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

full?(state)

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

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

new(opts \\ [])

@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(inflight)

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

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

packets_to_resend(inflight)

@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(state, pid, expected_type)

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

Pops a pending SUBSCRIBE or UNSUBSCRIBE request.

set_receive_maximum(state, max)

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

Updates the negotiated server receive_maximum (MQTT 5.0 CONNACK).

take_expired_for_retry(state, retry_interval_ms, now \\ System.monotonic_time(:millisecond))

@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(state, pid, type, from, payload \\ nil)

@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(state, pid)

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

Records an incoming QoS 2 PUBLISH.

track_outgoing_publish(state, packet, ack_target)

@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.