Skip to content

Commit

Permalink
Set durable annotation for MQTT messages
Browse files Browse the repository at this point in the history
This is a follow up to #11012

 ## What?
For incoming MQTT messages, always set the `durable` message container
annotation.

 ## Why?
Even though defaulting to `durable=true` when no durable annotation is
set, as prior to this commit, is good enough, explicitly setting the
durable annotation makes the code a bit more future proof and
maintainable going forward in 4.0 where we will rely more on the durable
annotation because AMQP 1.0 message headers will be omitted in classic
and quorum queues (see #10964)

For MQTT messages, it's important to know whether the message was
published with QoS 0 or QoS 1 because it affects the QoS for the MQTT
message that will delivered to the MQTT subscriber.

The performance impact of always setting the durable annotation is
negligible.
  • Loading branch information
ansd committed Apr 22, 2024
1 parent 11a4348 commit 553a3d7
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions deps/rabbitmq_mqtt/src/mc_mqtt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@
]).

init(Msg = #mqtt_msg{qos = Qos,
props = Props})
when is_integer(Qos) ->
Anns0 = case Qos > 0 of
true ->
#{};
false ->
#{?ANN_DURABLE => false}
end,
props = Props}) ->
Anns0 = #{?ANN_DURABLE => durable(Qos)},
Anns1 = case Props of
#{'Message-Expiry-Interval' := Seconds} ->
Anns0#{ttl => timer:seconds(Seconds),
Expand Down Expand Up @@ -290,7 +284,7 @@ convert_to(mc_amqp, #mqtt_msg{qos = Qos,
[] -> S2;
_ -> [#'v1_0.message_annotations'{content = MsgAnns} | S2]
end,
S = [#'v1_0.header'{durable = Qos > 0} | S3],
S = [#'v1_0.header'{durable = durable(Qos)} | S3],
mc_amqp:convert_from(mc_amqp, S, Env);
convert_to(mc_amqpl, #mqtt_msg{qos = Qos,
props = Props,
Expand Down Expand Up @@ -559,3 +553,6 @@ amqp_encode(Data, Acc0) ->
Bin = amqp10_framing:encode_bin(Data),
Acc = setelement(5, Acc0, [Bin | element(5, Acc0)]),
setelement(7, Acc, ?CONTENT_TYPE_AMQP).

durable(?QOS_0) -> false;
durable(?QOS_1) -> true.

0 comments on commit 553a3d7

Please sign in to comment.