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
@type ack_target() :: {:sync, GenServer.from()} | {:async, pid(), (term() -> any()) | nil} | nil
@type control_entry() :: %{ type: :subscribe | :unsubscribe, from: GenServer.from(), payload: term() }
@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() }
@type outgoing_state() :: :puback_pending | :pubrec_pending | :pubcomp_pending
@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
@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.
@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.
@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.
Allocates the next unused Packet Identifier (1..65_535).
@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.
Completes an incoming QoS 2 PUBLISH upon receiving PUBREL.
@spec effective_limit(t()) :: pos_integer()
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.
Options
:max_inflight- Client-side maximum concurrent QoS 1/2 publishes (default65_535):receive_maximum- Server-advertised Receive Maximum (default65_535)
@spec outgoing_count(t()) :: non_neg_integer()
Returns the number of active outgoing QoS 1/2 publishes.
@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).
@spec pop_control(t(), 1..65535, :subscribe | :unsubscribe) :: {:ok, control_entry(), t()} | :error
Pops a pending SUBSCRIBE or UNSUBSCRIBE request.
@spec set_receive_maximum(t(), pos_integer()) :: t()
Updates the negotiated server receive_maximum (MQTT 5.0 CONNACK).
@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.
@spec track_control( t(), 1..65535, :subscribe | :unsubscribe, GenServer.from(), term() ) :: t()
Tracks a pending SUBSCRIBE or UNSUBSCRIBE request with its payload.
Records an incoming QoS 2 PUBLISH.
@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.