Skip to content

Commit e576dd7

Browse files
committed
Set durable annotation for MQTT messages
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.
1 parent 11a4348 commit e576dd7

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

deps/rabbitmq_mqtt/src/mc_mqtt.erl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,8 @@
2323
]).
2424

2525
init(Msg = #mqtt_msg{qos = Qos,
26-
props = Props})
27-
when is_integer(Qos) ->
28-
Anns0 = case Qos > 0 of
29-
true ->
30-
#{};
31-
false ->
32-
#{?ANN_DURABLE => false}
33-
end,
26+
props = Props}) ->
27+
Anns0 = #{?ANN_DURABLE => durable(Qos)},
3428
Anns1 = case Props of
3529
#{'Message-Expiry-Interval' := Seconds} ->
3630
Anns0#{ttl => timer:seconds(Seconds),
@@ -290,7 +284,7 @@ convert_to(mc_amqp, #mqtt_msg{qos = Qos,
290284
[] -> S2;
291285
_ -> [#'v1_0.message_annotations'{content = MsgAnns} | S2]
292286
end,
293-
S = [#'v1_0.header'{durable = Qos > 0} | S3],
287+
S = [#'v1_0.header'{durable = durable(Qos)} | S3],
294288
mc_amqp:convert_from(mc_amqp, S, Env);
295289
convert_to(mc_amqpl, #mqtt_msg{qos = Qos,
296290
props = Props,
@@ -559,3 +553,6 @@ amqp_encode(Data, Acc0) ->
559553
Bin = amqp10_framing:encode_bin(Data),
560554
Acc = setelement(5, Acc0, [Bin | element(5, Acc0)]),
561555
setelement(7, Acc, ?CONTENT_TYPE_AMQP).
556+
557+
durable(?QOS_0) -> false;
558+
durable(?QOS_1) -> true.

0 commit comments

Comments
 (0)