Rename the 'client_id' field to 'clientid'

This commit is contained in:
Feng Lee 2019-09-24 17:06:25 +08:00
parent 1a5c10bd6f
commit 20ddd498fc
26 changed files with 165 additions and 151 deletions

View File

@ -28,7 +28,7 @@
%% APIs %% APIs
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(authenticate(emqx_types:client_info()) -spec(authenticate(emqx_types:clientinfo())
-> {ok, #{auth_result := emqx_types:auth_result(), -> {ok, #{auth_result := emqx_types:auth_result(),
anonymous := boolean}} | {error, term()}). anonymous := boolean}} | {error, term()}).
authenticate(Client) -> authenticate(Client) ->

View File

@ -86,7 +86,7 @@ bin(B) when is_binary(B) ->
B. B.
%% @doc Match access rule %% @doc Match access rule
-spec(match(emqx_types:client_info(), emqx_types:topic(), rule()) -spec(match(emqx_types:clientinfo(), emqx_types:topic(), rule())
-> {matched, allow} | {matched, deny} | nomatch). -> {matched, allow} | {matched, deny} | nomatch).
match(_ClientInfo, _Topic, {AllowDeny, all}) when ?ALLOW_DENY(AllowDeny) -> match(_ClientInfo, _Topic, {AllowDeny, all}) when ?ALLOW_DENY(AllowDeny) ->
{matched, AllowDeny}; {matched, AllowDeny};
@ -104,7 +104,7 @@ match_who(_ClientInfo, {user, all}) ->
true; true;
match_who(_ClientInfo, {client, all}) -> match_who(_ClientInfo, {client, all}) ->
true; true;
match_who(#{client_id := ClientId}, {client, ClientId}) -> match_who(#{clientid := ClientId}, {client, ClientId}) ->
true; true;
match_who(#{username := Username}, {user, Username}) -> match_who(#{username := Username}, {user, Username}) ->
true; true;
@ -142,9 +142,9 @@ feed_var(ClientInfo, Pattern) ->
feed_var(ClientInfo, Pattern, []). feed_var(ClientInfo, Pattern, []).
feed_var(_ClientInfo, [], Acc) -> feed_var(_ClientInfo, [], Acc) ->
lists:reverse(Acc); lists:reverse(Acc);
feed_var(ClientInfo = #{client_id := undefined}, [<<"%c">>|Words], Acc) -> feed_var(ClientInfo = #{clientid := undefined}, [<<"%c">>|Words], Acc) ->
feed_var(ClientInfo, Words, [<<"%c">>|Acc]); feed_var(ClientInfo, Words, [<<"%c">>|Acc]);
feed_var(ClientInfo = #{client_id := ClientId}, [<<"%c">>|Words], Acc) -> feed_var(ClientInfo = #{clientid := ClientId}, [<<"%c">>|Words], Acc) ->
feed_var(ClientInfo, Words, [ClientId |Acc]); feed_var(ClientInfo, Words, [ClientId |Acc]);
feed_var(ClientInfo = #{username := undefined}, [<<"%u">>|Words], Acc) -> feed_var(ClientInfo = #{username := undefined}, [<<"%u">>|Words], Acc) ->
feed_var(ClientInfo, Words, [<<"%u">>|Acc]); feed_var(ClientInfo, Words, [<<"%u">>|Acc]);

View File

@ -73,11 +73,11 @@ start_link() ->
-spec(stop() -> ok). -spec(stop() -> ok).
stop() -> gen_server:stop(?MODULE). stop() -> gen_server:stop(?MODULE).
-spec(check(emqx_types:client_info()) -> boolean()). -spec(check(emqx_types:clientinfo()) -> boolean()).
check(#{client_id := ClientId, check(#{clientid := ClientId,
username := Username, username := Username,
peerhost := IPAddr}) -> peerhost := IPAddr}) ->
ets:member(?BANNED_TAB, {client_id, ClientId}) ets:member(?BANNED_TAB, {clientid, ClientId})
orelse ets:member(?BANNED_TAB, {username, Username}) orelse ets:member(?BANNED_TAB, {username, Username})
orelse ets:member(?BANNED_TAB, {ipaddr, IPAddr}). orelse ets:member(?BANNED_TAB, {ipaddr, IPAddr}).
@ -85,7 +85,7 @@ check(#{client_id := ClientId,
add(Banned) when is_record(Banned, banned) -> add(Banned) when is_record(Banned, banned) ->
mnesia:dirty_write(?BANNED_TAB, Banned). mnesia:dirty_write(?BANNED_TAB, Banned).
-spec(delete({client_id, emqx_types:client_id()} -spec(delete({clientid, emqx_types:clientid()}
| {username, emqx_types:username()} | {username, emqx_types:username()}
| {peerhost, emqx_types:peerhost()}) -> ok). | {peerhost, emqx_types:peerhost()}) -> ok).
delete(Key) -> mnesia:dirty_delete(?BANNED_TAB, Key). delete(Key) -> mnesia:dirty_delete(?BANNED_TAB, Key).

View File

@ -94,19 +94,19 @@ start_link() ->
%% @doc Register a channel. %% @doc Register a channel.
%% Channel will be unregistered automatically when the channel process dies %% Channel will be unregistered automatically when the channel process dies
-spec(register_channel(emqx_types:client_id()) -> ok). -spec(register_channel(emqx_types:clientid()) -> ok).
register_channel(ClientId) when is_binary(ClientId) -> register_channel(ClientId) when is_binary(ClientId) ->
register_channel(ClientId, self()). register_channel(ClientId, self()).
%% @doc Register a channel with pid. %% @doc Register a channel with pid.
-spec(register_channel(emqx_types:client_id(), chan_pid()) -> ok). -spec(register_channel(emqx_types:clientid(), chan_pid()) -> ok).
register_channel(ClientId, ChanPid) -> register_channel(ClientId, ChanPid) ->
Chan = {ClientId, ChanPid}, Chan = {ClientId, ChanPid},
true = ets:insert(?CHAN_TAB, Chan), true = ets:insert(?CHAN_TAB, Chan),
ok = emqx_cm_registry:register_channel(Chan), ok = emqx_cm_registry:register_channel(Chan),
cast({registered, Chan}). cast({registered, Chan}).
-spec(unregister_channel(emqx_types:client_id()) -> ok). -spec(unregister_channel(emqx_types:clientid()) -> ok).
unregister_channel(ClientId) when is_binary(ClientId) -> unregister_channel(ClientId) when is_binary(ClientId) ->
true = do_unregister_channel({ClientId, self()}), true = do_unregister_channel({ClientId, self()}),
ok. ok.
@ -119,31 +119,31 @@ do_unregister_channel(Chan) ->
true = ets:delete(?CHAN_STATS_TAB, Chan), true = ets:delete(?CHAN_STATS_TAB, Chan),
ets:delete_object(?CHAN_TAB, Chan). ets:delete_object(?CHAN_TAB, Chan).
%% @doc Get attrs of a channel. %% @doc Get info of a channel.
-spec(get_chan_attrs(emqx_types:client_id()) -> maybe(emqx_types:attrs())). -spec(get_chan_attrs(emqx_types:clientid()) -> maybe(emqx_types:attrs())).
get_chan_attrs(ClientId) -> get_chan_attrs(ClientId) ->
with_channel(ClientId, fun(ChanPid) -> get_chan_attrs(ClientId, ChanPid) end). with_channel(ClientId, fun(ChanPid) -> get_chan_attrs(ClientId, ChanPid) end).
-spec(get_chan_attrs(emqx_types:client_id(), chan_pid()) -> maybe(emqx_types:attrs())). -spec(get_chan_attrs(emqx_types:clientid(), chan_pid()) -> maybe(emqx_types:attrs())).
get_chan_attrs(ClientId, ChanPid) when node(ChanPid) == node() -> get_chan_attrs(ClientId, ChanPid) when node(ChanPid) == node() ->
Chan = {ClientId, ChanPid}, Chan = {ClientId, ChanPid},
emqx_tables:lookup_value(?CHAN_ATTRS_TAB, Chan); emqx_tables:lookup_value(?CHAN_ATTRS_TAB, Chan);
get_chan_attrs(ClientId, ChanPid) -> get_chan_attrs(ClientId, ChanPid) ->
rpc_call(node(ChanPid), get_chan_attrs, [ClientId, ChanPid]). rpc_call(node(ChanPid), get_chan_attrs, [ClientId, ChanPid]).
%% @doc Set attrs of a channel. %% @doc Set info of a channel.
-spec(set_chan_attrs(emqx_types:client_id(), emqx_types:attrs()) -> ok). -spec(set_chan_attrs(emqx_types:clientid(), emqx_types:attrs()) -> ok).
set_chan_attrs(ClientId, Attrs) when is_binary(ClientId) -> set_chan_attrs(ClientId, Info) when is_binary(ClientId) ->
Chan = {ClientId, self()}, Chan = {ClientId, self()},
true = ets:insert(?CHAN_ATTRS_TAB, {Chan, Attrs}), true = ets:insert(?CHAN_ATTRS_TAB, {Chan, Info}),
ok. ok.
%% @doc Get channel's stats. %% @doc Get channel's stats.
-spec(get_chan_stats(emqx_types:client_id()) -> maybe(emqx_types:stats())). -spec(get_chan_stats(emqx_types:clientid()) -> maybe(emqx_types:stats())).
get_chan_stats(ClientId) -> get_chan_stats(ClientId) ->
with_channel(ClientId, fun(ChanPid) -> get_chan_stats(ClientId, ChanPid) end). with_channel(ClientId, fun(ChanPid) -> get_chan_stats(ClientId, ChanPid) end).
-spec(get_chan_stats(emqx_types:client_id(), chan_pid()) -> maybe(emqx_types:stats())). -spec(get_chan_stats(emqx_types:clientid(), chan_pid()) -> maybe(emqx_types:stats())).
get_chan_stats(ClientId, ChanPid) when node(ChanPid) == node() -> get_chan_stats(ClientId, ChanPid) when node(ChanPid) == node() ->
Chan = {ClientId, ChanPid}, Chan = {ClientId, ChanPid},
emqx_tables:lookup_value(?CHAN_STATS_TAB, Chan); emqx_tables:lookup_value(?CHAN_STATS_TAB, Chan);
@ -151,23 +151,23 @@ get_chan_stats(ClientId, ChanPid) ->
rpc_call(node(ChanPid), get_chan_stats, [ClientId, ChanPid]). rpc_call(node(ChanPid), get_chan_stats, [ClientId, ChanPid]).
%% @doc Set channel's stats. %% @doc Set channel's stats.
-spec(set_chan_stats(emqx_types:client_id(), emqx_types:stats()) -> ok). -spec(set_chan_stats(emqx_types:clientid(), emqx_types:stats()) -> ok).
set_chan_stats(ClientId, Stats) when is_binary(ClientId) -> set_chan_stats(ClientId, Stats) when is_binary(ClientId) ->
set_chan_stats(ClientId, self(), Stats). set_chan_stats(ClientId, self(), Stats).
-spec(set_chan_stats(emqx_types:client_id(), chan_pid(), emqx_types:stats()) -> ok). -spec(set_chan_stats(emqx_types:clientid(), chan_pid(), emqx_types:stats()) -> ok).
set_chan_stats(ClientId, ChanPid, Stats) -> set_chan_stats(ClientId, ChanPid, Stats) ->
Chan = {ClientId, ChanPid}, Chan = {ClientId, ChanPid},
true = ets:insert(?CHAN_STATS_TAB, {Chan, Stats}), true = ets:insert(?CHAN_STATS_TAB, {Chan, Stats}),
ok. ok.
%% @doc Open a session. %% @doc Open a session.
-spec(open_session(boolean(), emqx_types:client_info(), emqx_types:conninfo()) -spec(open_session(boolean(), emqx_types:clientinfo(), emqx_types:conninfo())
-> {ok, #{session := emqx_session:session(), -> {ok, #{session := emqx_session:session(),
present := boolean(), present := boolean(),
pendings => list()}} pendings => list()}}
| {error, Reason :: term()}). | {error, Reason :: term()}).
open_session(true, ClientInfo = #{client_id := ClientId}, ConnInfo) -> open_session(true, ClientInfo = #{clientid := ClientId}, ConnInfo) ->
CleanStart = fun(_) -> CleanStart = fun(_) ->
ok = discard_session(ClientId), ok = discard_session(ClientId),
Session = emqx_session:init(ClientInfo, ConnInfo), Session = emqx_session:init(ClientInfo, ConnInfo),
@ -175,7 +175,7 @@ open_session(true, ClientInfo = #{client_id := ClientId}, ConnInfo) ->
end, end,
emqx_cm_locker:trans(ClientId, CleanStart); emqx_cm_locker:trans(ClientId, CleanStart);
open_session(false, ClientInfo = #{client_id := ClientId}, ConnInfo) -> open_session(false, ClientInfo = #{clientid := ClientId}, ConnInfo) ->
ResumeStart = fun(_) -> ResumeStart = fun(_) ->
case takeover_session(ClientId) of case takeover_session(ClientId) of
{ok, ConnMod, ChanPid, Session} -> {ok, ConnMod, ChanPid, Session} ->
@ -192,7 +192,7 @@ open_session(false, ClientInfo = #{client_id := ClientId}, ConnInfo) ->
emqx_cm_locker:trans(ClientId, ResumeStart). emqx_cm_locker:trans(ClientId, ResumeStart).
%% @doc Try to takeover a session. %% @doc Try to takeover a session.
-spec(takeover_session(emqx_types:client_id()) -spec(takeover_session(emqx_types:clientid())
-> {ok, emqx_session:session()} | {error, Reason :: term()}). -> {ok, emqx_session:session()} | {error, Reason :: term()}).
takeover_session(ClientId) -> takeover_session(ClientId) ->
case lookup_channels(ClientId) of case lookup_channels(ClientId) of
@ -221,7 +221,7 @@ takeover_session(ClientId, ChanPid) ->
rpc_call(node(ChanPid), takeover_session, [ClientId, ChanPid]). rpc_call(node(ChanPid), takeover_session, [ClientId, ChanPid]).
%% @doc Discard all the sessions identified by the ClientId. %% @doc Discard all the sessions identified by the ClientId.
-spec(discard_session(emqx_types:client_id()) -> ok). -spec(discard_session(emqx_types:clientid()) -> ok).
discard_session(ClientId) when is_binary(ClientId) -> discard_session(ClientId) when is_binary(ClientId) ->
case lookup_channels(ClientId) of case lookup_channels(ClientId) of
[] -> ok; [] -> ok;
@ -259,12 +259,12 @@ with_channel(ClientId, Fun) ->
end. end.
%% @doc Lookup channels. %% @doc Lookup channels.
-spec(lookup_channels(emqx_types:client_id()) -> list(chan_pid())). -spec(lookup_channels(emqx_types:clientid()) -> list(chan_pid())).
lookup_channels(ClientId) -> lookup_channels(ClientId) ->
lookup_channels(global, ClientId). lookup_channels(global, ClientId).
%% @doc Lookup local or global channels. %% @doc Lookup local or global channels.
-spec(lookup_channels(local | global, emqx_types:client_id()) -> list(chan_pid())). -spec(lookup_channels(local | global, emqx_types:clientid()) -> list(chan_pid())).
lookup_channels(global, ClientId) -> lookup_channels(global, ClientId) ->
case emqx_cm_registry:is_enabled() of case emqx_cm_registry:is_enabled() of
true -> true ->

View File

@ -32,11 +32,11 @@
start_link() -> start_link() ->
ekka_locker:start_link(?MODULE). ekka_locker:start_link(?MODULE).
-spec(trans(emqx_types:client_id(), fun(([node()]) -> any())) -> any()). -spec(trans(emqx_types:clientid(), fun(([node()]) -> any())) -> any()).
trans(ClientId, Fun) -> trans(ClientId, Fun) ->
trans(ClientId, Fun, undefined). trans(ClientId, Fun, undefined).
-spec(trans(maybe(emqx_types:client_id()), -spec(trans(maybe(emqx_types:clientid()),
fun(([node()])-> any()), ekka_locker:piggyback()) -> any()). fun(([node()])-> any()), ekka_locker:piggyback()) -> any()).
trans(undefined, Fun, _Piggyback) -> trans(undefined, Fun, _Piggyback) ->
Fun([]); Fun([]);
@ -48,15 +48,15 @@ trans(ClientId, Fun, Piggyback) ->
{error, client_id_unavailable} {error, client_id_unavailable}
end. end.
-spec(lock(emqx_types:client_id()) -> ekka_locker:lock_result()). -spec(lock(emqx_types:clientid()) -> ekka_locker:lock_result()).
lock(ClientId) -> lock(ClientId) ->
ekka_locker:acquire(?MODULE, ClientId, strategy()). ekka_locker:acquire(?MODULE, ClientId, strategy()).
-spec(lock(emqx_types:client_id(), ekka_locker:piggyback()) -> ekka_locker:lock_result()). -spec(lock(emqx_types:clientid(), ekka_locker:piggyback()) -> ekka_locker:lock_result()).
lock(ClientId, Piggyback) -> lock(ClientId, Piggyback) ->
ekka_locker:acquire(?MODULE, ClientId, strategy(), Piggyback). ekka_locker:acquire(?MODULE, ClientId, strategy(), Piggyback).
-spec(unlock(emqx_types:client_id()) -> {boolean(), [node()]}). -spec(unlock(emqx_types:clientid()) -> {boolean(), [node()]}).
unlock(ClientId) -> unlock(ClientId) ->
ekka_locker:release(?MODULE, ClientId, strategy()). ekka_locker:release(?MODULE, ClientId, strategy()).

View File

@ -65,8 +65,8 @@ is_enabled() ->
emqx:get_env(enable_channel_registry, true). emqx:get_env(enable_channel_registry, true).
%% @doc Register a global channel. %% @doc Register a global channel.
-spec(register_channel(emqx_types:client_id() -spec(register_channel(emqx_types:clientid()
| {emqx_types:client_id(), pid()}) -> ok). | {emqx_types:clientid(), pid()}) -> ok).
register_channel(ClientId) when is_binary(ClientId) -> register_channel(ClientId) when is_binary(ClientId) ->
register_channel({ClientId, self()}); register_channel({ClientId, self()});
@ -77,8 +77,8 @@ register_channel({ClientId, ChanPid}) when is_binary(ClientId), is_pid(ChanPid)
end. end.
%% @doc Unregister a global channel. %% @doc Unregister a global channel.
-spec(unregister_channel(emqx_types:client_id() -spec(unregister_channel(emqx_types:clientid()
| {emqx_types:client_id(), pid()}) -> ok). | {emqx_types:clientid(), pid()}) -> ok).
unregister_channel(ClientId) when is_binary(ClientId) -> unregister_channel(ClientId) when is_binary(ClientId) ->
unregister_channel({ClientId, self()}); unregister_channel({ClientId, self()});
@ -89,7 +89,7 @@ unregister_channel({ClientId, ChanPid}) when is_binary(ClientId), is_pid(ChanPid
end. end.
%% @doc Lookup the global channels. %% @doc Lookup the global channels.
-spec(lookup_channels(emqx_types:client_id()) -> list(pid())). -spec(lookup_channels(emqx_types:clientid()) -> list(pid())).
lookup_channels(ClientId) -> lookup_channels(ClientId) ->
[ChanPid || #channel{pid = ChanPid} <- mnesia:dirty_read(?TAB, ClientId)]. [ChanPid || #channel{pid = ChanPid} <- mnesia:dirty_read(?TAB, ClientId)].

View File

@ -51,7 +51,7 @@
}). }).
-record(flapping, { -record(flapping, {
client_id :: emqx_types:client_id(), clientid :: emqx_types:clientid(),
peerhost :: emqx_types:peerhost(), peerhost :: emqx_types:peerhost(),
started_at :: pos_integer(), started_at :: pos_integer(),
detect_cnt :: pos_integer(), detect_cnt :: pos_integer(),
@ -69,8 +69,8 @@ start_link() ->
stop() -> gen_server:stop(?MODULE). stop() -> gen_server:stop(?MODULE).
%% @doc Check flapping when a MQTT client connected. %% @doc Check flapping when a MQTT client connected.
-spec(check(emqx_types:client_info()) -> boolean()). -spec(check(emqx_types:clientinfo()) -> boolean()).
check(#{client_id := ClientId}) -> check(#{clientid := ClientId}) ->
check(ClientId, get_policy()). check(ClientId, get_policy()).
check(ClientId, #{banned_interval := Interval}) -> check(ClientId, #{banned_interval := Interval}) ->
@ -81,10 +81,10 @@ check(ClientId, #{banned_interval := Interval}) ->
end. end.
%% @doc Detect flapping when a MQTT client disconnected. %% @doc Detect flapping when a MQTT client disconnected.
-spec(detect(emqx_types:client_info()) -> boolean()). -spec(detect(emqx_types:clientinfo()) -> boolean()).
detect(Client) -> detect(Client, get_policy()). detect(Client) -> detect(Client, get_policy()).
detect(#{client_id := ClientId, peerhost := PeerHost}, detect(#{clientid := ClientId, peerhost := PeerHost},
Policy = #{threshold := Threshold}) -> Policy = #{threshold := Threshold}) ->
try ets:update_counter(?FLAPPING_TAB, ClientId, {#flapping.detect_cnt, 1}) of try ets:update_counter(?FLAPPING_TAB, ClientId, {#flapping.detect_cnt, 1}) of
Cnt when Cnt < Threshold -> false; Cnt when Cnt < Threshold -> false;
@ -97,7 +97,7 @@ detect(#{client_id := ClientId, peerhost := PeerHost},
catch catch
error:badarg -> error:badarg ->
%% Create a flapping record. %% Create a flapping record.
Flapping = #flapping{client_id = ClientId, Flapping = #flapping{clientid = ClientId,
peerhost = PeerHost, peerhost = PeerHost,
started_at = emqx_time:now_ms(), started_at = emqx_time:now_ms(),
detect_cnt = 1 detect_cnt = 1
@ -131,7 +131,7 @@ handle_call(Req, _From, State) ->
?LOG(error, "Unexpected call: ~p", [Req]), ?LOG(error, "Unexpected call: ~p", [Req]),
{reply, ignored, State}. {reply, ignored, State}.
handle_cast({detected, Flapping = #flapping{client_id = ClientId, handle_cast({detected, Flapping = #flapping{clientid = ClientId,
peerhost = PeerHost, peerhost = PeerHost,
started_at = StartedAt, started_at = StartedAt,
detect_cnt = DetectCnt}, detect_cnt = DetectCnt},
@ -142,7 +142,7 @@ handle_cast({detected, Flapping = #flapping{client_id = ClientId,
?LOG(error, "Flapping detected: ~s(~s) disconnected ~w times in ~wms", ?LOG(error, "Flapping detected: ~s(~s) disconnected ~w times in ~wms",
[ClientId, esockd_net:ntoa(PeerHost), DetectCnt, Duration]), [ClientId, esockd_net:ntoa(PeerHost), DetectCnt, Duration]),
%% Banned. %% Banned.
BannedFlapping = Flapping#flapping{client_id = {banned, ClientId}, BannedFlapping = Flapping#flapping{clientid = {banned, ClientId},
banned_at = emqx_time:now_ms() banned_at = emqx_time:now_ms()
}, },
alarm_handler:set_alarm({{flapping_detected, ClientId}, BannedFlapping}), alarm_handler:set_alarm({{flapping_detected, ClientId}, BannedFlapping}),
@ -192,11 +192,11 @@ expire_flapping(NowTime, #{duration := Duration, banned_interval := Interval}) -
case ets:select(?FLAPPING_TAB, case ets:select(?FLAPPING_TAB,
[{#flapping{started_at = '$1', banned_at = undefined, _ = '_'}, [{#flapping{started_at = '$1', banned_at = undefined, _ = '_'},
[{'<', '$1', NowTime-Duration}], ['$_']}, [{'<', '$1', NowTime-Duration}], ['$_']},
{#flapping{client_id = {banned, '_'}, banned_at = '$1', _ = '_'}, {#flapping{clientid = {banned, '_'}, banned_at = '$1', _ = '_'},
[{'<', '$1', NowTime-Interval}], ['$_']}]) of [{'<', '$1', NowTime-Interval}], ['$_']}]) of
[] -> ok; [] -> ok;
Flappings -> Flappings ->
lists:foreach(fun(Flapping = #flapping{client_id = {banned, ClientId}}) -> lists:foreach(fun(Flapping = #flapping{clientid = {banned, ClientId}}) ->
ets:delete_object(?FLAPPING_TAB, Flapping), ets:delete_object(?FLAPPING_TAB, Flapping),
alarm_handler:clear_alarm({flapping_detected, ClientId}); alarm_handler:clear_alarm({flapping_detected, ClientId});
(_) -> ok (_) -> ok

View File

@ -180,7 +180,7 @@ parse_packet(#mqtt_packet_header{type = ?CONNECT}, FrameBin, _Options) ->
will_retain = bool(WillRetain), will_retain = bool(WillRetain),
keepalive = KeepAlive, keepalive = KeepAlive,
properties = Properties, properties = Properties,
client_id = ClientId}, clientid = ClientId},
{ConnPacket1, Rest5} = parse_will_message(ConnPacket, Rest4), {ConnPacket1, Rest5} = parse_will_message(ConnPacket, Rest4),
{Username, Rest6} = parse_utf8_string(Rest5, bool(UsernameFlag)), {Username, Rest6} = parse_utf8_string(Rest5, bool(UsernameFlag)),
{Passsword, <<>>} = parse_utf8_string(Rest6, bool(PasswordFlag)), {Passsword, <<>>} = parse_utf8_string(Rest6, bool(PasswordFlag)),
@ -435,7 +435,7 @@ serialize_variable(#mqtt_packet_connect{
will_retain = WillRetain, will_retain = WillRetain,
keepalive = KeepAlive, keepalive = KeepAlive,
properties = Properties, properties = Properties,
client_id = ClientId, clientid = ClientId,
will_props = WillProps, will_props = WillProps,
will_topic = WillTopic, will_topic = WillTopic,
will_payload = WillPayload, will_payload = WillPayload,

View File

@ -38,7 +38,7 @@
%% Configs %% Configs
-export([ set_metadata_peername/1 -export([ set_metadata_peername/1
, set_metadata_client_id/1 , set_metadata_clientid/1
, set_proc_metadata/1 , set_proc_metadata/1
, set_primary_log_level/1 , set_primary_log_level/1
, set_log_handler_level/2 , set_log_handler_level/2
@ -121,11 +121,11 @@ critical(Format, Args) ->
critical(Metadata, Format, Args) when is_map(Metadata) -> critical(Metadata, Format, Args) when is_map(Metadata) ->
logger:critical(Format, Args, Metadata). logger:critical(Format, Args, Metadata).
-spec(set_metadata_client_id(emqx_types:client_id()) -> ok). -spec(set_metadata_clientid(emqx_types:clientid()) -> ok).
set_metadata_client_id(<<>>) -> set_metadata_clientid(<<>>) ->
ok; ok;
set_metadata_client_id(ClientId) -> set_metadata_clientid(ClientId) ->
set_proc_metadata(#{client_id => ClientId}). set_proc_metadata(#{clientid => ClientId}).
-spec(set_metadata_peername(peername_str()) -> ok). -spec(set_metadata_peername(peername_str()) -> ok).
set_metadata_peername(Peername) -> set_metadata_peername(Peername) ->

View File

@ -71,13 +71,13 @@
make(Topic, Payload) -> make(Topic, Payload) ->
make(undefined, Topic, Payload). make(undefined, Topic, Payload).
-spec(make(atom() | emqx_types:client_id(), -spec(make(atom() | emqx_types:clientid(),
emqx_topic:topic(), emqx_topic:topic(),
emqx_types:payload()) -> emqx_types:message()). emqx_types:payload()) -> emqx_types:message()).
make(From, Topic, Payload) -> make(From, Topic, Payload) ->
make(From, ?QOS_0, Topic, Payload). make(From, ?QOS_0, Topic, Payload).
-spec(make(atom() | emqx_types:client_id(), -spec(make(atom() | emqx_types:clientid(),
emqx_types:qos(), emqx_types:qos(),
emqx_topic:topic(), emqx_topic:topic(),
emqx_types:payload()) -> emqx_types:message()). emqx_types:payload()) -> emqx_types:message()).

View File

@ -61,7 +61,7 @@ all_rules() ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Check ACL %% @doc Check ACL
-spec(check_acl(emqx_types:client_info(), emqx_types:pubsub(), emqx_topic:topic(), -spec(check_acl(emqx_types:clientinfo(), emqx_types:pubsub(), emqx_topic:topic(),
emqx_access_rule:acl_result(), acl_rules()) emqx_access_rule:acl_result(), acl_rules())
-> {ok, allow} | {ok, deny} | ok). -> {ok, allow} | {ok, deny} | ok).
check_acl(Client, PubSub, Topic, _AclResult, Rules) -> check_acl(Client, PubSub, Topic, _AclResult, Rules) ->

View File

@ -88,8 +88,8 @@ on_client_disconnected(ClientInfo, Reason, ConnInfo, Env) ->
?LOG(error, "Failed to encode 'disconnected' presence: ~p", [Presence]) ?LOG(error, "Failed to encode 'disconnected' presence: ~p", [Presence])
end. end.
clientid(#{client_id := undefined}, #{client_id := ClientId}) -> ClientId; clientid(#{clientid := undefined}, #{clientid := ClientId}) -> ClientId;
clientid(#{client_id := ClientId}, _ConnInfo) -> ClientId. clientid(#{clientid := ClientId}, _ConnInfo) -> ClientId.
username(#{username := undefined}, #{username := Username}) -> Username; username(#{username := undefined}, #{username := Username}) -> Username;
username(#{username := Username}, _ConnInfo) -> Username. username(#{username := Username}, _ConnInfo) -> Username.

View File

@ -36,8 +36,8 @@
load(Topics) -> load(Topics) ->
emqx_hooks:add('client.connected', {?MODULE, on_client_connected, [Topics]}). emqx_hooks:add('client.connected', {?MODULE, on_client_connected, [Topics]}).
on_client_connected(#{client_id := ClientId, on_client_connected(#{clientid := ClientId,
username := Username}, ?RC_SUCCESS, _ConnInfo, Topics) -> username := Username}, ?RC_SUCCESS, _ConnInfo, Topics) ->
Replace = fun(Topic) -> Replace = fun(Topic) ->
rep(<<"%u">>, Username, rep(<<"%c">>, ClientId, Topic)) rep(<<"%u">>, Username, rep(<<"%c">>, ClientId, Topic))
end, end,

View File

@ -66,7 +66,7 @@ unmount(MountPoint, Msg = #message{topic = Topic}) ->
-spec(replvar(maybe(mountpoint()), map()) -> maybe(mountpoint())). -spec(replvar(maybe(mountpoint()), map()) -> maybe(mountpoint())).
replvar(undefined, _Vars) -> replvar(undefined, _Vars) ->
undefined; undefined;
replvar(MountPoint, #{client_id := ClientId, username := Username}) -> replvar(MountPoint, #{clientid := ClientId, username := Username}) ->
lists:foldl(fun feed_var/2, MountPoint, lists:foldl(fun feed_var/2, MountPoint,
[{<<"%c">>, ClientId}, {<<"%u">>, Username}]). [{<<"%c">>, ClientId}, {<<"%u">>, Username}]).

View File

@ -181,16 +181,16 @@ check_proto_ver(#mqtt_packet_connect{proto_ver = Ver,
%% MQTT3.1 does not allow null clientId %% MQTT3.1 does not allow null clientId
check_client_id(#mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V3, check_client_id(#mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V3,
client_id = <<>>}, _Opts) -> clientid = <<>>}, _Opts) ->
{error, ?RC_CLIENT_IDENTIFIER_NOT_VALID}; {error, ?RC_CLIENT_IDENTIFIER_NOT_VALID};
%% Issue#599: Null clientId and clean_start = false %% Issue#599: Null clientId and clean_start = false
check_client_id(#mqtt_packet_connect{client_id = <<>>, check_client_id(#mqtt_packet_connect{clientid = <<>>,
clean_start = false}, _Opts) -> clean_start = false}, _Opts) ->
{error, ?RC_CLIENT_IDENTIFIER_NOT_VALID}; {error, ?RC_CLIENT_IDENTIFIER_NOT_VALID};
check_client_id(#mqtt_packet_connect{client_id = <<>>, check_client_id(#mqtt_packet_connect{clientid = <<>>,
clean_start = true}, _Opts) -> clean_start = true}, _Opts) ->
ok; ok;
check_client_id(#mqtt_packet_connect{client_id = ClientId}, check_client_id(#mqtt_packet_connect{clientid = ClientId},
_Opts = #{max_clientid_len := MaxLen}) -> _Opts = #{max_clientid_len := MaxLen}) ->
case (1 =< (Len = byte_size(ClientId))) andalso (Len =< MaxLen) of case (1 =< (Len = byte_size(ClientId))) andalso (Len =< MaxLen) of
true -> ok; true -> ok;
@ -240,8 +240,8 @@ validate_topic_filters(TopicFilters) ->
end, TopicFilters). end, TopicFilters).
%% @doc Publish Packet to Message. %% @doc Publish Packet to Message.
-spec(to_message(emqx_types:client_info(), emqx_ypes:packet()) -> emqx_types:message()). -spec(to_message(emqx_types:clientinfo(), emqx_ypes:packet()) -> emqx_types:message()).
to_message(#{client_id := ClientId, username := Username, peerhost := PeerHost}, to_message(#{clientid := ClientId, username := Username, peerhost := PeerHost},
#mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
retain = Retain, retain = Retain,
qos = QoS, qos = QoS,
@ -257,7 +257,7 @@ to_message(#{client_id := ClientId, username := Username, peerhost := PeerHost},
-spec(will_msg(#mqtt_packet_connect{}) -> emqx_types:message()). -spec(will_msg(#mqtt_packet_connect{}) -> emqx_types:message()).
will_msg(#mqtt_packet_connect{will_flag = false}) -> will_msg(#mqtt_packet_connect{will_flag = false}) ->
undefined; undefined;
will_msg(#mqtt_packet_connect{client_id = ClientId, will_msg(#mqtt_packet_connect{clientid = ClientId,
username = Username, username = Username,
will_retain = Retain, will_retain = Retain,
will_qos = QoS, will_qos = QoS,
@ -304,7 +304,7 @@ format_variable(#mqtt_packet_connect{
will_flag = WillFlag, will_flag = WillFlag,
clean_start = CleanStart, clean_start = CleanStart,
keepalive = KeepAlive, keepalive = KeepAlive,
client_id = ClientId, clientid = ClientId,
will_topic = WillTopic, will_topic = WillTopic,
will_payload = WillPayload, will_payload = WillPayload,
username = Username, username = Username,

View File

@ -28,16 +28,16 @@
, stop_trace/1 , stop_trace/1
]). ]).
-type(trace_who() :: {client_id | topic, binary() | list()}). -type(trace_who() :: {clientid | topic, binary() | list()}).
-define(TRACER, ?MODULE). -define(TRACER, ?MODULE).
-define(FORMAT, {emqx_logger_formatter, -define(FORMAT, {emqx_logger_formatter,
#{template => #{template =>
[time," [",level,"] ", [time," [",level,"] ",
{client_id, {clientid,
[{peername, [{peername,
[client_id,"@",peername," "], [clientid,"@",peername," "],
[client_id, " "]}], [clientid, " "]}],
[{peername, [{peername,
[peername," "], [peername," "],
[]}]}, []}]},
@ -45,7 +45,7 @@
-define(TOPIC_TRACE_ID(T), "trace_topic_"++T). -define(TOPIC_TRACE_ID(T), "trace_topic_"++T).
-define(CLIENT_TRACE_ID(C), "trace_clientid_"++C). -define(CLIENT_TRACE_ID(C), "trace_clientid_"++C).
-define(TOPIC_TRACE(T), {topic,T}). -define(TOPIC_TRACE(T), {topic,T}).
-define(CLIENT_TRACE(C), {client_id,C}). -define(CLIENT_TRACE(C), {clientid,C}).
-define(is_log_level(L), -define(is_log_level(L),
L =:= emergency orelse L =:= emergency orelse
@ -67,7 +67,7 @@ trace(publish, #message{from = From, topic = Topic, payload = Payload})
when is_binary(From); is_atom(From) -> when is_binary(From); is_atom(From) ->
emqx_logger:info(#{topic => Topic, mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY} }, "PUBLISH to ~s: ~p", [Topic, Payload]). emqx_logger:info(#{topic => Topic, mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY} }, "PUBLISH to ~s: ~p", [Topic, Payload]).
%% @doc Start to trace client_id or topic. %% @doc Start to trace clientid or topic.
-spec(start_trace(trace_who(), logger:level(), string()) -> ok | {error, term()}). -spec(start_trace(trace_who(), logger:level(), string()) -> ok | {error, term()}).
start_trace(Who, all, LogFile) -> start_trace(Who, all, LogFile) ->
start_trace(Who, debug, LogFile); start_trace(Who, debug, LogFile);
@ -87,7 +87,7 @@ start_trace(Who, Level, LogFile) ->
false -> {error, {invalid_log_level, Level}} false -> {error, {invalid_log_level, Level}}
end. end.
%% @doc Stop tracing client_id or topic. %% @doc Stop tracing clientid or topic.
-spec(stop_trace(trace_who()) -> ok | {error, term()}). -spec(stop_trace(trace_who()) -> ok | {error, term()}).
stop_trace(Who) -> stop_trace(Who) ->
uninstall_trance_handler(Who). uninstall_trance_handler(Who).

View File

@ -32,8 +32,8 @@
]). ]).
-export_type([ conninfo/0 -export_type([ conninfo/0
, client_info/0 , clientinfo/0
, client_id/0 , clientid/0
, username/0 , username/0
, password/0 , password/0
, peerhost/0 , peerhost/0
@ -78,6 +78,7 @@
]). ]).
-export_type([ caps/0 -export_type([ caps/0
, attrs/0
, infos/0 , infos/0
, stats/0 , stats/0
]). ]).
@ -97,28 +98,39 @@
-type(socktype() :: tcp | udp | ssl | proxy | atom()). -type(socktype() :: tcp | udp | ssl | proxy | atom()).
-type(conninfo() :: #{socktype := socktype(), -type(conninfo() :: #{socktype := socktype(),
peername := peername(),
sockname := peername(), sockname := peername(),
peername := peername(),
peercert := esockd_peercert:peercert(), peercert := esockd_peercert:peercert(),
conn_mod := module(), conn_mod := module(),
atom() => term() proto_name := binary(),
proto_ver := ver(),
clean_start := boolean(),
clientid := clientid(),
username := username(),
conn_props := properties(),
connected := boolean(),
connected_at := erlang:timestamp(),
keepalive := 0..16#FFFF,
receive_maximum := non_neg_integer(),
expiry_interval := non_neg_integer(),
atom() => term()
}). }).
-type(client_info() :: #{zone := zone(), -type(clientinfo() :: #{zone := zone(),
protocol := protocol(), protocol := protocol(),
peerhost := peerhost(), peerhost := peerhost(),
client_id := client_id(), clientid := clientid(),
username := username(), username := username(),
peercert := esockd_peercert:peercert(), peercert := esockd_peercert:peercert(),
is_bridge := boolean(), is_bridge := boolean(),
is_superuser := boolean(), is_superuser := boolean(),
mountpoint := maybe(binary()), mountpoint := maybe(binary()),
ws_cookie := maybe(list()), ws_cookie := maybe(list()),
password => maybe(binary()), password => maybe(binary()),
auth_result => auth_result(), auth_result => auth_result(),
anonymous => boolean(), anonymous => boolean(),
atom() => term() atom() => term()
}). }).
-type(client_id() :: binary()|atom()). -type(clientid() :: binary()|atom()).
-type(username() :: maybe(binary())). -type(username() :: maybe(binary())).
-type(password() :: maybe(binary())). -type(password() :: maybe(binary())).
-type(peerhost() :: inet:ip_address()). -type(peerhost() :: inet:ip_address()).
@ -167,6 +179,7 @@
-type(command() :: #command{}). -type(command() :: #command{}).
-type(caps() :: emqx_mqtt_caps:caps()). -type(caps() :: emqx_mqtt_caps:caps()).
-type(attrs() :: #{atom() => term()}).
-type(infos() :: #{atom() => term()}). -type(infos() :: #{atom() => term()}).
-type(stats() :: #{atom() => non_neg_integer()|stats()}). -type(stats() :: #{atom() => non_neg_integer()|stats()}).

View File

@ -113,7 +113,7 @@ t_reload_acl(_) ->
t_check_acl_1(_) -> t_check_acl_1(_) ->
Client = #{zone => external, Client = #{zone => external,
client_id => <<"client1">>, clientid => <<"client1">>,
username => <<"testuser">> username => <<"testuser">>
}, },
allow = ?AC:check_acl(Client, subscribe, <<"users/testuser/1">>), allow = ?AC:check_acl(Client, subscribe, <<"users/testuser/1">>),
@ -124,14 +124,14 @@ t_check_acl_1(_) ->
t_check_acl_2(_) -> t_check_acl_2(_) ->
Client = #{zone => external, Client = #{zone => external,
client_id => <<"client2">>, clientid => <<"client2">>,
username => <<"xyz">> username => <<"xyz">>
}, },
deny = ?AC:check_acl(Client, subscribe, <<"a/b/c">>). deny = ?AC:check_acl(Client, subscribe, <<"a/b/c">>).
t_acl_cache_basic(_) -> t_acl_cache_basic(_) ->
Client = #{zone => external, Client = #{zone => external,
client_id => <<"client1">>, clientid => <<"client1">>,
username => <<"testuser">> username => <<"testuser">>
}, },
not_found = ?CACHE:get_acl_cache(subscribe, <<"users/testuser/1">>), not_found = ?CACHE:get_acl_cache(subscribe, <<"users/testuser/1">>),
@ -146,7 +146,7 @@ t_acl_cache_basic(_) ->
t_acl_cache_expiry(_) -> t_acl_cache_expiry(_) ->
application:set_env(emqx, acl_cache_ttl, 100), application:set_env(emqx, acl_cache_ttl, 100),
Client = #{zone => external, Client = #{zone => external,
client_id => <<"client1">>, clientid => <<"client1">>,
username => <<"testuser">> username => <<"testuser">>
}, },
allow = ?AC:check_acl(Client, subscribe, <<"clients/client1">>), allow = ?AC:check_acl(Client, subscribe, <<"clients/client1">>),
@ -157,7 +157,7 @@ t_acl_cache_expiry(_) ->
t_acl_cache_full(_) -> t_acl_cache_full(_) ->
application:set_env(emqx, acl_cache_max_size, 1), application:set_env(emqx, acl_cache_max_size, 1),
Client = #{zone => external, Client = #{zone => external,
client_id => <<"client1">>, clientid => <<"client1">>,
username => <<"testuser">> username => <<"testuser">>
}, },
allow = ?AC:check_acl(Client, subscribe, <<"users/testuser/1">>), allow = ?AC:check_acl(Client, subscribe, <<"users/testuser/1">>),
@ -173,7 +173,7 @@ t_acl_cache_cleanup(_) ->
application:set_env(emqx, acl_cache_ttl, 100), application:set_env(emqx, acl_cache_ttl, 100),
application:set_env(emqx, acl_cache_max_size, 2), application:set_env(emqx, acl_cache_max_size, 2),
Client = #{zone => external, Client = #{zone => external,
client_id => <<"client1">>, clientid => <<"client1">>,
username => <<"testuser">> username => <<"testuser">>
}, },
allow = ?AC:check_acl(Client, subscribe, <<"users/testuser/1">>), allow = ?AC:check_acl(Client, subscribe, <<"users/testuser/1">>),
@ -345,12 +345,12 @@ t_compile_rule(_) ->
t_match_rule(_) -> t_match_rule(_) ->
ClientInfo1 = #{zone => external, ClientInfo1 = #{zone => external,
client_id => <<"testClient">>, clientid => <<"testClient">>,
username => <<"TestUser">>, username => <<"TestUser">>,
peerhost => {127,0,0,1} peerhost => {127,0,0,1}
}, },
ClientInfo2 = #{zone => external, ClientInfo2 = #{zone => external,
client_id => <<"testClient">>, clientid => <<"testClient">>,
username => <<"TestUser">>, username => <<"TestUser">>,
peerhost => {192,168,0,10} peerhost => {192,168,0,10}
}, },

View File

@ -37,7 +37,7 @@ end_per_suite(_Config) ->
ekka_mnesia:delete_schema(). ekka_mnesia:delete_schema().
t_add_delete(_) -> t_add_delete(_) ->
Banned = #banned{who = {client_id, <<"TestClient">>}, Banned = #banned{who = {clientid, <<"TestClient">>},
reason = <<"test">>, reason = <<"test">>,
by = <<"banned suite">>, by = <<"banned suite">>,
desc = <<"test">>, desc = <<"test">>,
@ -45,27 +45,27 @@ t_add_delete(_) ->
}, },
ok = emqx_banned:add(Banned), ok = emqx_banned:add(Banned),
?assertEqual(1, emqx_banned:info(size)), ?assertEqual(1, emqx_banned:info(size)),
ok = emqx_banned:delete({client_id, <<"TestClient">>}), ok = emqx_banned:delete({clientid, <<"TestClient">>}),
?assertEqual(0, emqx_banned:info(size)). ?assertEqual(0, emqx_banned:info(size)).
t_check(_) -> t_check(_) ->
ok = emqx_banned:add(#banned{who = {client_id, <<"BannedClient">>}}), ok = emqx_banned:add(#banned{who = {clientid, <<"BannedClient">>}}),
ok = emqx_banned:add(#banned{who = {username, <<"BannedUser">>}}), ok = emqx_banned:add(#banned{who = {username, <<"BannedUser">>}}),
ok = emqx_banned:add(#banned{who = {ipaddr, {192,168,0,1}}}), ok = emqx_banned:add(#banned{who = {ipaddr, {192,168,0,1}}}),
?assertEqual(3, emqx_banned:info(size)), ?assertEqual(3, emqx_banned:info(size)),
ClientInfo1 = #{client_id => <<"BannedClient">>, ClientInfo1 = #{clientid => <<"BannedClient">>,
username => <<"user">>, username => <<"user">>,
peerhost => {127,0,0,1} peerhost => {127,0,0,1}
}, },
ClientInfo2 = #{client_id => <<"client">>, ClientInfo2 = #{clientid => <<"client">>,
username => <<"BannedUser">>, username => <<"BannedUser">>,
peerhost => {127,0,0,1} peerhost => {127,0,0,1}
}, },
ClientInfo3 = #{client_id => <<"client">>, ClientInfo3 = #{clientid => <<"client">>,
username => <<"user">>, username => <<"user">>,
peerhost => {192,168,0,1} peerhost => {192,168,0,1}
}, },
ClientInfo4 = #{client_id => <<"client">>, ClientInfo4 = #{clientid => <<"client">>,
username => <<"user">>, username => <<"user">>,
peerhost => {127,0,0,1} peerhost => {127,0,0,1}
}, },
@ -73,7 +73,7 @@ t_check(_) ->
?assert(emqx_banned:check(ClientInfo2)), ?assert(emqx_banned:check(ClientInfo2)),
?assert(emqx_banned:check(ClientInfo3)), ?assert(emqx_banned:check(ClientInfo3)),
?assertNot(emqx_banned:check(ClientInfo4)), ?assertNot(emqx_banned:check(ClientInfo4)),
ok = emqx_banned:delete({client_id, <<"BannedClient">>}), ok = emqx_banned:delete({clientid, <<"BannedClient">>}),
ok = emqx_banned:delete({username, <<"BannedUser">>}), ok = emqx_banned:delete({username, <<"BannedUser">>}),
ok = emqx_banned:delete({ipaddr, {192,168,0,1}}), ok = emqx_banned:delete({ipaddr, {192,168,0,1}}),
?assertNot(emqx_banned:check(ClientInfo1)), ?assertNot(emqx_banned:check(ClientInfo1)),
@ -84,7 +84,7 @@ t_check(_) ->
t_unused(_) -> t_unused(_) ->
{ok, Banned} = emqx_banned:start_link(), {ok, Banned} = emqx_banned:start_link(),
ok = emqx_banned:add(#banned{who = {client_id, <<"BannedClient">>}, ok = emqx_banned:add(#banned{who = {clientid, <<"BannedClient">>},
until = erlang:system_time(second) until = erlang:system_time(second)
}), }),
?assertEqual(ignored, gen_server:call(Banned, unexpected_req)), ?assertEqual(ignored, gen_server:call(Banned, unexpected_req)),

View File

@ -63,17 +63,17 @@ t_get_set_chan_stats(_) ->
t_open_session(_) -> t_open_session(_) ->
ClientInfo = #{zone => external, ClientInfo = #{zone => external,
client_id => <<"clientid">>, clientid => <<"clientid">>,
username => <<"username">>, username => <<"username">>,
peerhost => {127,0,0,1}}, peerhost => {127,0,0,1}},
ConnInfo = #{peername => {{127,0,0,1}, 5000}, ConnInfo = #{peername => {{127,0,0,1}, 5000},
receive_maximum => 100}, receive_maximum => 100},
{ok, #{session := Session1, present := false}} {ok, #{session := Session1, present := false}}
= emqx_cm:open_session(true, ClientInfo, ConnInfo), = emqx_cm:open_session(true, ClientInfo, ConnInfo),
?assertEqual(100, emqx_session:info(max_inflight, Session1)), ?assertEqual(100, emqx_session:info(inflight_max, Session1)),
{ok, #{session := Session2, present := false}} {ok, #{session := Session2, present := false}}
= emqx_cm:open_session(false, ClientInfo, ConnInfo), = emqx_cm:open_session(false, ClientInfo, ConnInfo),
?assertEqual(100, emqx_session:info(max_inflight, Session2)). ?assertEqual(100, emqx_session:info(inflight_max, Session2)).
t_discard_session(_) -> t_discard_session(_) ->
ok = emqx_cm:discard_session(<<"clientid">>). ok = emqx_cm:discard_session(<<"clientid">>).

View File

@ -41,7 +41,7 @@ end_per_suite(_Config) ->
t_detect_check(_) -> t_detect_check(_) ->
ClientInfo = #{zone => external, ClientInfo = #{zone => external,
client_id => <<"clientid">>, clientid => <<"clientid">>,
peerhost => {127,0,0,1} peerhost => {127,0,0,1}
}, },
false = emqx_flapping:detect(ClientInfo), false = emqx_flapping:detect(ClientInfo),

View File

@ -147,7 +147,7 @@ prop_serialize_parse_connect() ->
Packet = ?CONNECT_PACKET(#mqtt_packet_connect{ Packet = ?CONNECT_PACKET(#mqtt_packet_connect{
proto_name = ProtoName, proto_name = ProtoName,
proto_ver = ProtoVer, proto_ver = ProtoVer,
client_id = <<"clientId">>, clientid = <<"clientId">>,
will_qos = ?QOS_1, will_qos = ?QOS_1,
will_flag = true, will_flag = true,
will_retain = true, will_retain = true,
@ -167,7 +167,7 @@ t_serialize_parse_v3_connect(_) ->
Packet = ?CONNECT_PACKET( Packet = ?CONNECT_PACKET(
#mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V3, #mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V3,
proto_name = <<"MQIsdp">>, proto_name = <<"MQIsdp">>,
client_id = <<"mosqpub/10451-iMac.loca">>, clientid = <<"mosqpub/10451-iMac.loca">>,
clean_start = true, clean_start = true,
keepalive = 60 keepalive = 60
}), }),
@ -180,7 +180,7 @@ t_serialize_parse_v4_connect(_) ->
Packet = ?CONNECT_PACKET( Packet = ?CONNECT_PACKET(
#mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V4, #mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V4,
proto_name = <<"MQTT">>, proto_name = <<"MQTT">>,
client_id = <<"mosqpub/10451-iMac.loca">>, clientid = <<"mosqpub/10451-iMac.loca">>,
clean_start = true, clean_start = true,
keepalive = 60 keepalive = 60
}), }),
@ -213,7 +213,7 @@ t_serialize_parse_v5_connect(_) ->
proto_ver = ?MQTT_PROTO_V5, proto_ver = ?MQTT_PROTO_V5,
is_bridge = false, is_bridge = false,
clean_start = true, clean_start = true,
client_id = <<>>, clientid = <<>>,
will_flag = true, will_flag = true,
will_qos = ?QOS_1, will_qos = ?QOS_1,
will_retain = false, will_retain = false,
@ -231,7 +231,7 @@ t_serialize_parse_connect_without_clientid(_) ->
Bin = <<16,12,0,4,77,81,84,84,4,2,0,60,0,0>>, Bin = <<16,12,0,4,77,81,84,84,4,2,0,60,0,0>>,
Packet = ?CONNECT_PACKET(#mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V4, Packet = ?CONNECT_PACKET(#mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V4,
proto_name = <<"MQTT">>, proto_name = <<"MQTT">>,
client_id = <<>>, clientid = <<>>,
clean_start = true, clean_start = true,
keepalive = 60 keepalive = 60
}), }),
@ -246,7 +246,7 @@ t_serialize_parse_connect_with_will(_) ->
Packet = #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT}, Packet = #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT},
variable = #mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V3, variable = #mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V3,
proto_name = <<"MQIsdp">>, proto_name = <<"MQIsdp">>,
client_id = <<"mosqpub/10452-iMac.loca">>, clientid = <<"mosqpub/10452-iMac.loca">>,
clean_start = true, clean_start = true,
keepalive = 60, keepalive = 60,
will_retain = false, will_retain = false,
@ -267,7 +267,7 @@ t_serialize_parse_bridge_connect(_) ->
67,58,50,57,58,50,66,58,55,55,58,53,50,47,115,116,97,116,101,0,1,48>>, 67,58,50,57,58,50,66,58,55,55,58,53,50,47,115,116,97,116,101,0,1,48>>,
Topic = <<"$SYS/broker/connection/C_00:0C:29:2B:77:52/state">>, Topic = <<"$SYS/broker/connection/C_00:0C:29:2B:77:52/state">>,
Packet = #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT}, Packet = #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT},
variable = #mqtt_packet_connect{client_id = <<"C_00:0C:29:2B:77:52">>, variable = #mqtt_packet_connect{clientid = <<"C_00:0C:29:2B:77:52">>,
proto_ver = 16#03, proto_ver = 16#03,
proto_name = <<"MQIsdp">>, proto_name = <<"MQIsdp">>,
is_bridge = true, is_bridge = true,

View File

@ -56,12 +56,12 @@ t_replvar(_) ->
?assertEqual(undefined, replvar(undefined, #{})), ?assertEqual(undefined, replvar(undefined, #{})),
?assertEqual(<<"mount/user/clientid/">>, ?assertEqual(<<"mount/user/clientid/">>,
replvar(<<"mount/%u/%c/">>, replvar(<<"mount/%u/%c/">>,
#{client_id => <<"clientid">>, #{clientid => <<"clientid">>,
username => <<"user">> username => <<"user">>
})), })),
?assertEqual(<<"mount/%u/clientid/">>, ?assertEqual(<<"mount/%u/clientid/">>,
replvar(<<"mount/%u/%c/">>, replvar(<<"mount/%u/%c/">>,
#{client_id => <<"clientid">>, #{clientid => <<"clientid">>,
username => undefined username => undefined
})). })).

View File

@ -42,8 +42,8 @@ t_message_expiry_interval_2(_) ->
emqtt:stop(ClientA). emqtt:stop(ClientA).
message_expiry_interval_init() -> message_expiry_interval_init() ->
{ok, ClientA} = emqtt:start_link([{proto_ver,v5}, {client_id, <<"client-a">>}, {clean_start, false},{properties, #{'Session-Expiry-Interval' => 360}}]), {ok, ClientA} = emqtt:start_link([{proto_ver,v5}, {clientid, <<"client-a">>}, {clean_start, false},{properties, #{'Session-Expiry-Interval' => 360}}]),
{ok, ClientB} = emqtt:start_link([{proto_ver,v5}, {client_id, <<"client-b">>}, {clean_start, false},{properties, #{'Session-Expiry-Interval' => 360}}]), {ok, ClientB} = emqtt:start_link([{proto_ver,v5}, {clientid, <<"client-b">>}, {clean_start, false},{properties, #{'Session-Expiry-Interval' => 360}}]),
{ok, _} = emqtt:connect(ClientA), {ok, _} = emqtt:connect(ClientA),
{ok, _} = emqtt:connect(ClientB), {ok, _} = emqtt:connect(ClientB),
%% subscribe and disconnect client-b %% subscribe and disconnect client-b
@ -58,7 +58,7 @@ message_expiry_interval_exipred(ClientA, QoS) ->
ct:sleep(1500), ct:sleep(1500),
%% resume the session for client-b %% resume the session for client-b
{ok, ClientB1} = emqtt:start_link([{proto_ver,v5}, {client_id, <<"client-b">>}, {clean_start, false},{properties, #{'Session-Expiry-Interval' => 360}}]), {ok, ClientB1} = emqtt:start_link([{proto_ver,v5}, {clientid, <<"client-b">>}, {clean_start, false},{properties, #{'Session-Expiry-Interval' => 360}}]),
{ok, _} = emqtt:connect(ClientB1), {ok, _} = emqtt:connect(ClientB1),
%% verify client-b could not receive the publish message %% verify client-b could not receive the publish message
@ -78,7 +78,7 @@ message_expiry_interval_not_exipred(ClientA, QoS) ->
%% wait for 1s and then resume the session for client-b, the message should not expires %% wait for 1s and then resume the session for client-b, the message should not expires
%% as Message-Expiry-Interval = 20s %% as Message-Expiry-Interval = 20s
ct:sleep(1000), ct:sleep(1000),
{ok, ClientB1} = emqtt:start_link([{proto_ver,v5}, {client_id, <<"client-b">>}, {clean_start, false},{properties, #{'Session-Expiry-Interval' => 360}}]), {ok, ClientB1} = emqtt:start_link([{proto_ver,v5}, {clientid, <<"client-b">>}, {clean_start, false},{properties, #{'Session-Expiry-Interval' => 360}}]),
{ok, _} = emqtt:connect(ClientB1), {ok, _} = emqtt:connect(ClientB1),
%% verify client-b could receive the publish message and the Message-Expiry-Interval is set %% verify client-b could receive the publish message and the Message-Expiry-Interval is set

View File

@ -112,11 +112,11 @@ t_check_connect(_) ->
ConnPkt2 = #mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V3, ConnPkt2 = #mqtt_packet_connect{proto_ver = ?MQTT_PROTO_V3,
proto_name = <<"MQIsdp">>, proto_name = <<"MQIsdp">>,
client_id = <<>> clientid = <<>>
}, },
{error, ?RC_CLIENT_IDENTIFIER_NOT_VALID} = emqx_packet:check(ConnPkt2, Opts), {error, ?RC_CLIENT_IDENTIFIER_NOT_VALID} = emqx_packet:check(ConnPkt2, Opts),
ConnPkt3 = #mqtt_packet_connect{client_id = <<"123456">>}, ConnPkt3 = #mqtt_packet_connect{clientid = <<"123456">>},
{error, ?RC_CLIENT_IDENTIFIER_NOT_VALID} = emqx_packet:check(ConnPkt3, Opts), {error, ?RC_CLIENT_IDENTIFIER_NOT_VALID} = emqx_packet:check(ConnPkt3, Opts),
ConnPkt4 = #mqtt_packet_connect{will_flag = true, ConnPkt4 = #mqtt_packet_connect{will_flag = true,
@ -152,7 +152,7 @@ t_from_to_message(_) ->
packet_id = 10, packet_id = 10,
properties = #{}}, properties = #{}},
payload = <<"payload">>}, payload = <<"payload">>},
MsgFromPkt = emqx_packet:to_message(#{client_id => <<"clientid">>, MsgFromPkt = emqx_packet:to_message(#{clientid => <<"clientid">>,
username => <<"test">>, username => <<"test">>,
peerhost => {127,0,0,1}}, Pkt), peerhost => {127,0,0,1}}, Pkt),
?assertEqual(ExpectedMsg2, MsgFromPkt#message{id = emqx_message:id(ExpectedMsg), ?assertEqual(ExpectedMsg2, MsgFromPkt#message{id = emqx_message:id(ExpectedMsg),
@ -161,7 +161,7 @@ t_from_to_message(_) ->
t_will_msg(_) -> t_will_msg(_) ->
Pkt = #mqtt_packet_connect{will_flag = true, Pkt = #mqtt_packet_connect{will_flag = true,
client_id = <<"clientid">>, clientid = <<"clientid">>,
username = "test", username = "test",
will_retain = true, will_retain = true,
will_qos = ?QOS_2, will_qos = ?QOS_2,

View File

@ -35,18 +35,19 @@ end_per_suite(_Config) ->
t_start_traces(_Config) -> t_start_traces(_Config) ->
{ok, T} = emqtt:start_link([{host, "localhost"}, {ok, T} = emqtt:start_link([{host, "localhost"},
{client_id, <<"client">>}, {clientid, <<"client">>},
{username, <<"testuser">>}, {username, <<"testuser">>},
{password, <<"pass">>}]), {password, <<"pass">>}
]),
emqtt:connect(T), emqtt:connect(T),
%% Start tracing %% Start tracing
emqx_logger:set_log_level(error), emqx_logger:set_log_level(error),
{error, _} = emqx_tracer:start_trace({client_id, <<"client">>}, debug, "tmp/client.log"), {error, _} = emqx_tracer:start_trace({clientid, <<"client">>}, debug, "tmp/client.log"),
emqx_logger:set_log_level(debug), emqx_logger:set_log_level(debug),
ok = emqx_tracer:start_trace({client_id, <<"client">>}, debug, "tmp/client.log"), ok = emqx_tracer:start_trace({clientid, <<"client">>}, debug, "tmp/client.log"),
ok = emqx_tracer:start_trace({client_id, <<"client2">>}, all, "tmp/client2.log"), ok = emqx_tracer:start_trace({clientid, <<"client2">>}, all, "tmp/client2.log"),
{error, {invalid_log_level, bad_level}} = emqx_tracer:start_trace({client_id, <<"client3">>}, bad_level, "tmp/client3.log"), {error, {invalid_log_level, bad_level}} = emqx_tracer:start_trace({clientid, <<"client3">>}, bad_level, "tmp/client3.log"),
ok = emqx_tracer:start_trace({topic, <<"a/#">>}, all, "tmp/topic_trace.log"), ok = emqx_tracer:start_trace({topic, <<"a/#">>}, all, "tmp/topic_trace.log"),
ct:sleep(100), ct:sleep(100),
@ -56,8 +57,8 @@ t_start_traces(_Config) ->
?assert(filelib:is_regular("tmp/topic_trace.log")), ?assert(filelib:is_regular("tmp/topic_trace.log")),
%% Get current traces %% Get current traces
?assertEqual([{{client_id,"client"},{debug,"tmp/client.log"}}, ?assertEqual([{{clientid,"client"},{debug,"tmp/client.log"}},
{{client_id,"client2"},{debug,"tmp/client2.log"}}, {{clientid,"client2"},{debug,"tmp/client2.log"}},
{{topic,"a/#"},{debug,"tmp/topic_trace.log"}}], emqx_tracer:lookup_traces()), {{topic,"a/#"},{debug,"tmp/topic_trace.log"}}], emqx_tracer:lookup_traces()),
%% set the overall log level to debug %% set the overall log level to debug
@ -73,8 +74,8 @@ t_start_traces(_Config) ->
?assert(filelib:file_size("tmp/client2.log") == 0), ?assert(filelib:file_size("tmp/client2.log") == 0),
%% Stop tracing %% Stop tracing
ok = emqx_tracer:stop_trace({client_id, <<"client">>}), ok = emqx_tracer:stop_trace({clientid, <<"client">>}),
ok = emqx_tracer:stop_trace({client_id, <<"client2">>}), ok = emqx_tracer:stop_trace({clientid, <<"client2">>}),
ok = emqx_tracer:stop_trace({topic, <<"a/#">>}), ok = emqx_tracer:stop_trace({topic, <<"a/#">>}),
emqtt:disconnect(T), emqtt:disconnect(T),