refactor(emqx_license): log a different msg when license is not loaded

This commit is contained in:
Zaiming (Stone) Shi 2022-02-08 13:57:27 +01:00
parent 319d08678e
commit c936706a1f
4 changed files with 25 additions and 21 deletions

View File

@ -35,4 +35,5 @@
-define(EXPIRED_DAY, -90).
-define(ERR_EXPIRED, expired).
-endif.

View File

@ -3,6 +3,7 @@
%%--------------------------------------------------------------------
-module(emqx_license).
-include("emqx_license.hrl").
-include_lib("emqx/include/logger.hrl").
-include_lib("emqx/include/emqx_mqtt.hrl").
-include_lib("typerefl/include/types.hrl").
@ -67,19 +68,22 @@ update_key(Value) when is_binary(Value); is_list(Value) ->
%%------------------------------------------------------------------------------
check(_ConnInfo, AckProps) ->
#{max_connections := MaxClients} = emqx_license_checker:limits(),
case MaxClients of
0 ->
?SLOG(error, #{msg => "Connection rejected due to the license expiration"}),
case emqx_license_checker:limits() of
{ok, #{max_connections := ?ERR_EXPIRED}} ->
?SLOG(error, #{msg => "connection_rejected_due_to_license_expired"}),
{stop, {error, ?RC_QUOTA_EXCEEDED}};
_ ->
{ok, #{max_connections := MaxClients}} ->
case check_max_clients_exceeded(MaxClients) of
true ->
?SLOG(error, #{msg => "Connection rejected due to max clients limitation"}),
?SLOG(error, #{msg => "connection_rejected_due_to_license_limit_reached"}),
{stop, {error, ?RC_QUOTA_EXCEEDED}};
false ->
{ok, AckProps}
end
end;
{error, Reason} ->
?SLOG(error, #{msg => "connection_rejected_due_to_license_not_loaded",
reason => Reason}),
{stop, {error, ?RC_QUOTA_EXCEEDED}}
end.
%%------------------------------------------------------------------------------

View File

@ -27,7 +27,7 @@
%% API
%%------------------------------------------------------------------------------
-type limits() :: #{max_connections := non_neg_integer()}.
-type limits() :: #{max_connections := non_neg_integer() | ?ERR_EXPIRED}.
-spec start_link(emqx_license_parser:license()) -> {ok, pid()}.
start_link(LicenseFetcher) ->
@ -45,13 +45,14 @@ update(License) ->
dump() ->
gen_server:call(?MODULE, dump).
-spec limits() -> limits().
-spec limits() -> {ok, limits()} | {error, any()}.
limits() ->
try ets:lookup(?MODULE, limits) of
[{limits, Limits}] -> Limits;
_ -> default_limits()
[{limits, Limits}] -> {ok, Limits};
_ -> {error, no_license}
catch
error:badarg -> default_limits()
error : badarg ->
{error, no_license}
end.
%%------------------------------------------------------------------------------
@ -116,9 +117,7 @@ warn_evaluation(_License, _NeedRestrict) -> false.
warn_expiry(_License, NeedRestrict) -> NeedRestrict.
limits(License, false) -> #{max_connections => emqx_license_parser:max_connections(License)};
limits(_License, true) -> #{max_connections => 0}.
default_limits() -> #{max_connections => 0}.
limits(_License, true) -> #{max_connections => ?ERR_EXPIRED}.
days_left(License) ->
DateEnd = emqx_license_parser:expiry_date(License),

View File

@ -47,7 +47,7 @@ set_special_configs(_) -> ok.
%%------------------------------------------------------------------------------
t_default_limits(_Config) ->
?assertMatch(#{max_connections := 0}, emqx_license_checker:limits()).
?assertMatch({error, no_license}, emqx_license_checker:limits()).
t_dump(_Config) ->
License = mk_license(
@ -86,7 +86,7 @@ t_update(_Config) ->
#{} = emqx_license_checker:update(License),
?assertMatch(
#{max_connections := 123},
{ok, #{max_connections := 123}},
emqx_license_checker:limits()).
t_update_by_timer(_Config) ->
@ -122,7 +122,7 @@ t_expired_trial(_Config) ->
#{} = emqx_license_checker:update(License),
?assertMatch(
#{max_connections := 0},
{ok, #{max_connections := expired}},
emqx_license_checker:limits()).
t_overexpired_small_client(_Config) ->
@ -142,7 +142,7 @@ t_overexpired_small_client(_Config) ->
#{} = emqx_license_checker:update(License),
?assertMatch(
#{max_connections := 0},
{ok, #{max_connections := expired}},
emqx_license_checker:limits()).
t_overexpired_medium_client(_Config) ->
@ -162,7 +162,7 @@ t_overexpired_medium_client(_Config) ->
#{} = emqx_license_checker:update(License),
?assertMatch(
#{max_connections := 123},
{ok, #{max_connections := 123}},
emqx_license_checker:limits()).
t_recently_expired_small_client(_Config) ->
@ -182,7 +182,7 @@ t_recently_expired_small_client(_Config) ->
#{} = emqx_license_checker:update(License),
?assertMatch(
#{max_connections := 123},
{ok, #{max_connections := 123}},
emqx_license_checker:limits()).
t_unknown_calls(_Config) ->