Skip to content

Commit

Permalink
Merge pull request #12492 from zmstone/0208-fix-return-receive-maximu…
Browse files Browse the repository at this point in the history
…m-in-connack

fix(emqx_channel): return Receive-Maximum in CONNACK when no error
  • Loading branch information
zmstone committed Feb 9, 2024
2 parents 30855e7 + cd3cc41 commit cbebc59
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
14 changes: 8 additions & 6 deletions apps/emqx/src/emqx_channel.erl
Expand Up @@ -2007,14 +2007,15 @@ merge_default_subopts(SubOpts) ->
%%--------------------------------------------------------------------
%% Enrich ConnAck Caps

enrich_connack_caps(
AckProps,
?IS_MQTT_V5 = #channel{
enrich_connack_caps(AckProps, ?IS_MQTT_V5 = Channel) ->
#channel{
clientinfo = #{
zone := Zone
},
conninfo = #{
receive_maximum := ReceiveMaximum
}
}
) ->
} = Channel,
#{
max_packet_size := MaxPktSize,
max_qos_allowed := MaxQoS,
Expand All @@ -2029,7 +2030,8 @@ enrich_connack_caps(
'Topic-Alias-Maximum' => MaxAlias,
'Wildcard-Subscription-Available' => flag(Wildcard),
'Subscription-Identifier-Available' => 1,
'Shared-Subscription-Available' => flag(Shared)
'Shared-Subscription-Available' => flag(Shared),
'Receive-Maximum' => ReceiveMaximum
},
%% MQTT 5.0 - 3.2.2.3.4:
%% It is a Protocol Error to include Maximum QoS more than once,
Expand Down
29 changes: 20 additions & 9 deletions apps/emqx/test/emqx_client_SUITE.erl
Expand Up @@ -72,7 +72,7 @@ groups() ->
t_dollar_topics,
t_sub_non_utf8_topic
]},
{mqttv5, [non_parallel_tests], [t_basic_with_props_v5]},
{mqttv5, [non_parallel_tests], [t_basic_with_props_v5, t_v5_receive_maximim_in_connack]},
{others, [non_parallel_tests], [
t_username_as_clientid,
t_certcn_as_clientid_default_config_tls,
Expand Down Expand Up @@ -103,14 +103,14 @@ end_per_testcase(_Case, _Config) ->
%%--------------------------------------------------------------------

t_basic_v3(_) ->
t_basic([{proto_ver, v3}]).
run_basic([{proto_ver, v3}]).

%%--------------------------------------------------------------------
%% Test cases for MQTT v4
%%--------------------------------------------------------------------

t_basic_v4(_Config) ->
t_basic([{proto_ver, v4}]).
run_basic([{proto_ver, v4}]).

t_cm(_) ->
emqx_config:put_zone_conf(default, [mqtt, idle_timeout], 1000),
Expand Down Expand Up @@ -335,19 +335,30 @@ t_sub_non_utf8_topic(_) ->
%% Test cases for MQTT v5
%%--------------------------------------------------------------------

t_basic_with_props_v5(_) ->
t_basic([
v5_conn_props(ReceiveMaximum) ->
[
{proto_ver, v5},
{properties, #{'Receive-Maximum' => 4}}
]).
{properties, #{'Receive-Maximum' => ReceiveMaximum}}
].

t_basic_with_props_v5(_) ->
run_basic(v5_conn_props(4)).

t_v5_receive_maximim_in_connack(_) ->
ReceiveMaximum = 7,
{ok, C} = emqtt:start_link(v5_conn_props(ReceiveMaximum)),
{ok, Props} = emqtt:connect(C),
?assertMatch(#{'Receive-Maximum' := ReceiveMaximum}, Props),
ok = emqtt:disconnect(C),
ok.

%%--------------------------------------------------------------------
%% General test cases.
%%--------------------------------------------------------------------

t_basic(_Opts) ->
run_basic(Opts) ->
Topic = nth(1, ?TOPICS),
{ok, C} = emqtt:start_link([{proto_ver, v4}]),
{ok, C} = emqtt:start_link(Opts),
{ok, _} = emqtt:connect(C),
{ok, _, [1]} = emqtt:subscribe(C, Topic, qos1),
{ok, _, [2]} = emqtt:subscribe(C, Topic, qos2),
Expand Down
4 changes: 4 additions & 0 deletions changes/ce/fix-12492.en.md
@@ -0,0 +1,4 @@
Return `Receive-Maximum` in `CONNACK` for MQTT v5 clients.

EMQX takes the min value of client's `Receive-Maximum` and server's `max_inflight` config as the max number of inflight (unacknowledged) messages allowed.
Prior to this fix, the value was not sent back to the client in `CONNACK` message.

0 comments on commit cbebc59

Please sign in to comment.