fix: Limit interval between 0~65535

This commit is contained in:
zhongwencool 2021-11-22 16:05:39 +08:00
parent fdff3c5a53
commit 870af6df41
3 changed files with 19 additions and 4 deletions

View File

@ -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}.

View File

@ -336,8 +336,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) ->

View File

@ -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.