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(EXPIRED_DAY, -90).
-define(ERR_EXPIRED, expired).
-endif. -endif.

View File

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

View File

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

View File

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