Bladeren bron

Merge pull request #6238 from zhongwencool/fix-keepalive-ct-failed

fix: keepalive ct failed
zhongwencool 4 jaren geleden
bovenliggende
commit
f7a55d3f8d

+ 12 - 1
apps/emqx/src/emqx_keepalive.erl

@@ -74,7 +74,18 @@ check(NewVal, KeepAlive = #keepalive{statval = OldVal,
         true -> {error, timeout}
     end.
 
+%% from mqtt-v3.1.1 specific
+%% A Keep Alive value of zero (0) has the effect of turning off the keep alive mechanism.
+%% This means that, in this case, the Server is not required
+%% to disconnect the Client on the grounds of inactivity.
+%% Note that a Server is permitted to disconnect a Client that it determines
+%% to be inactive or non-responsive at any time,
+%% regardless of the Keep Alive value provided by that Client.
+%%  Non normative comment
+%%The actual value of the Keep Alive is application specific;
+%% typically this is a few minutes.
+%% The maximum value is (65535s) 18 hours 12 minutes and 15 seconds.
 %% @doc Update keepalive's interval
 -spec(set(interval, non_neg_integer(), keepalive()) -> keepalive()).
-set(interval, Interval, KeepAlive) ->
+set(interval, Interval, KeepAlive) when Interval >= 0 andalso Interval =< 65535000 ->
     KeepAlive#keepalive{interval = Interval}.

+ 4 - 2
apps/emqx_management/src/emqx_mgmt.erl

@@ -328,8 +328,10 @@ set_ratelimit_policy(ClientId, Policy) ->
 set_quota_policy(ClientId, Policy) ->
     call_client(ClientId, {quota, Policy}).
 
-set_keepalive(ClientId, Interval) ->
-    call_client(ClientId, {keepalive, Interval}).
+set_keepalive(ClientId, Interval)when Interval >= 0 andalso Interval =< 65535 ->
+    call_client(ClientId, {keepalive, Interval});
+set_keepalive(_ClientId, _Interval) ->
+    {error, <<"mqtt3.1.1 specification: keepalive must between 0~65535">>}.
 
 %% @private
 call_client(ClientId, Req) ->

+ 3 - 1
apps/emqx_management/src/emqx_mgmt_api_clients.erl

@@ -457,6 +457,7 @@ keepalive_api() ->
                 ],
             responses => #{
                 <<"404">> => emqx_mgmt_util:error_schema(<<"Client id not found">>),
+                <<"400">> => emqx_mgmt_util:error_schema(<<"">>, 'PARAMS_ERROR'),
                 <<"200">> => emqx_mgmt_util:schema(<<"ok">>)}}},
     {"/clients/:clientid/keepalive", Metadata, set_keepalive}.
 %%%==============================================================================================
@@ -509,7 +510,8 @@ set_keepalive(put, #{bindings := #{clientid := ClientID}, query_string := Query}
             Interval = binary_to_integer(Interval0),
             case emqx_mgmt:set_keepalive(emqx_mgmt_util:urldecode(ClientID), Interval) of
                 ok -> {200};
-                {error, not_found} ->{404, ?CLIENT_ID_NOT_FOUND}
+                {error, not_found} ->{404, ?CLIENT_ID_NOT_FOUND};
+                {error, Reason} -> {400, #{code => 'PARAMS_ERROR', message => Reason}}
             end
     end.
 

+ 2 - 3
apps/emqx_management/test/emqx_mgmt_clients_api_SUITE.erl

@@ -175,8 +175,7 @@ t_keepalive(_Config) ->
     {ok, Ok} = emqx_mgmt_api_test_util:request_api(put, Path, Query, AuthHeader, <<"">>),
     ?assertEqual("", Ok),
     [Pid] = emqx_cm:lookup_channels(list_to_binary(ClientId)),
-    State = sys:get_state(Pid),
-    ct:pal("~p~n", [State]),
-    ?assertEqual(11000, element(2, element(5, element(9, State)))),
+    #{conninfo := #{keepalive := Keepalive}} = emqx_connection:info(Pid),
+    ?assertEqual(11, Keepalive),
     emqtt:disconnect(C1),
     ok.