Merge branch 'develop' into introduce-new-bridge-impl

This commit is contained in:
Gilbert 2019-02-28 15:14:40 +08:00 committed by GitHub
commit 771f8c052a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 636 additions and 490 deletions

View File

@ -35,6 +35,8 @@
-define(ALERT(Format), ?LOG(alert, Format, [])). -define(ALERT(Format), ?LOG(alert, Format, [])).
-define(ALERT(Format, Args), ?LOG(alert, Format, Args)). -define(ALERT(Format, Args), ?LOG(alert, Format, Args)).
-define(LOG(Level, Format), ?LOG(Level, Format, [])).
-define(LOG(Level, Format, Args), -define(LOG(Level, Format, Args),
begin begin
(logger:log(Level,#{},#{report_cb => fun(_) -> {(Format), (Args)} end})) (logger:log(Level,#{},#{report_cb => fun(_) -> {(Format), (Args)} end}))

View File

@ -14,20 +14,22 @@
-module(emqx_connection). -module(emqx_connection).
-behaviour(gen_server). -behaviour(gen_statem).
-include("emqx.hrl"). -include("emqx.hrl").
-include("emqx_mqtt.hrl"). -include("emqx_mqtt.hrl").
-include("logger.hrl"). -include("logger.hrl").
-export([start_link/3]). -export([start_link/3]).
-export([info/1, attrs/1, stats/1]). -export([info/1]).
-export([attrs/1]).
-export([stats/1]).
-export([kick/1]). -export([kick/1]).
-export([session/1]). -export([session/1]).
%% gen_server callbacks %% gen_statem callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, -export([idle/3, connected/3]).
code_change/3]). -export([init/1, callback_mode/0, code_change/4, terminate/3]).
-record(state, { -record(state, {
transport, transport,
@ -37,7 +39,7 @@
conn_state, conn_state,
active_n, active_n,
proto_state, proto_state,
parser_state, parse_state,
gc_state, gc_state,
keepalive, keepalive,
enable_stats, enable_stats,
@ -48,17 +50,18 @@
idle_timeout idle_timeout
}). }).
-define(DEFAULT_ACTIVE_N, 100). -define(ACTIVE_N, 100).
-define(HANDLE(T, C, D), handle((T), (C), (D))).
-define(SOCK_STATS, [recv_oct, recv_cnt, send_oct, send_cnt, send_pend]). -define(SOCK_STATS, [recv_oct, recv_cnt, send_oct, send_cnt, send_pend]).
start_link(Transport, Socket, Options) -> start_link(Transport, Socket, Options) ->
{ok, proc_lib:spawn_link(?MODULE, init, [[Transport, Socket, Options]])}. {ok, proc_lib:spawn_link(?MODULE, init, [{Transport, Socket, Options}])}.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% API %% API
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% for debug %% For debug
info(CPid) when is_pid(CPid) -> info(CPid) when is_pid(CPid) ->
call(CPid, info); call(CPid, info);
@ -81,10 +84,12 @@ info(#state{transport = Transport,
ProtoInfo = emqx_protocol:info(ProtoState), ProtoInfo = emqx_protocol:info(ProtoState),
lists:usort(lists:append(ConnInfo, ProtoInfo)). lists:usort(lists:append(ConnInfo, ProtoInfo)).
rate_limit_info(undefined) -> #{}; rate_limit_info(undefined) ->
rate_limit_info(Limit) -> esockd_rate_limit:info(Limit). #{};
rate_limit_info(Limit) ->
esockd_rate_limit:info(Limit).
%% for dashboard %% For dashboard
attrs(CPid) when is_pid(CPid) -> attrs(CPid) when is_pid(CPid) ->
call(CPid, attrs); call(CPid, attrs);
@ -103,45 +108,48 @@ stats(CPid) when is_pid(CPid) ->
stats(#state{transport = Transport, stats(#state{transport = Transport,
socket = Socket, socket = Socket,
proto_state = ProtoState}) -> proto_state = ProtoState}) ->
lists:append([emqx_misc:proc_stats(), SockStats = case Transport:getstat(Socket, ?SOCK_STATS) of
emqx_protocol:stats(ProtoState),
case Transport:getstat(Socket, ?SOCK_STATS) of
{ok, Ss} -> Ss; {ok, Ss} -> Ss;
{error, _} -> [] {error, _} -> []
end]). end,
lists:append([SockStats,
emqx_misc:proc_stats(),
emqx_protocol:stats(ProtoState)]).
kick(CPid) -> call(CPid, kick). kick(CPid) ->
call(CPid, kick).
session(CPid) -> call(CPid, session). session(CPid) ->
call(CPid, session).
call(CPid, Req) -> call(CPid, Req) ->
gen_server:call(CPid, Req, infinity). gen_statem:call(CPid, Req, infinity).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% gen_server callbacks %% gen_statem callbacks
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
init([Transport, RawSocket, Options]) -> init({Transport, RawSocket, Options}) ->
case Transport:wait(RawSocket) of {ok, Socket} = Transport:wait(RawSocket),
{ok, Socket} ->
Zone = proplists:get_value(zone, Options),
{ok, Peername} = Transport:ensure_ok_or_exit(peername, [Socket]), {ok, Peername} = Transport:ensure_ok_or_exit(peername, [Socket]),
{ok, Sockname} = Transport:ensure_ok_or_exit(sockname, [Socket]), {ok, Sockname} = Transport:ensure_ok_or_exit(sockname, [Socket]),
Peercert = Transport:ensure_ok_or_exit(peercert, [Socket]), Peercert = Transport:ensure_ok_or_exit(peercert, [Socket]),
emqx_logger:set_metadata_peername(esockd_net:format(Peername)),
Zone = proplists:get_value(zone, Options),
RateLimit = init_limiter(proplists:get_value(rate_limit, Options)), RateLimit = init_limiter(proplists:get_value(rate_limit, Options)),
PubLimit = init_limiter(emqx_zone:get_env(Zone, publish_limit)), PubLimit = init_limiter(emqx_zone:get_env(Zone, publish_limit)),
ActiveN = proplists:get_value(active_n, Options, ?DEFAULT_ACTIVE_N), ActiveN = proplists:get_value(active_n, Options, ?ACTIVE_N),
EnableStats = emqx_zone:get_env(Zone, enable_stats, true), EnableStats = emqx_zone:get_env(Zone, enable_stats, true),
IdleTimout = emqx_zone:get_env(Zone, idle_timeout, 30000), IdleTimout = emqx_zone:get_env(Zone, idle_timeout, 30000),
SendFun = send_fun(Transport, Socket), SendFun = fun(Data) -> Transport:async_send(Socket, Data) end,
ProtoState = emqx_protocol:init(#{peername => Peername, ProtoState = emqx_protocol:init(#{peername => Peername,
sockname => Sockname, sockname => Sockname,
peercert => Peercert, peercert => Peercert,
sendfun => SendFun}, Options), sendfun => SendFun}, Options),
ParserState = emqx_protocol:parser(ProtoState), ParseState = emqx_protocol:parser(ProtoState),
GcPolicy = emqx_zone:get_env(Zone, force_gc_policy, false), GcPolicy = emqx_zone:get_env(Zone, force_gc_policy, false),
GcState = emqx_gc:init(GcPolicy), GcState = emqx_gc:init(GcPolicy),
State = run_socket(#state{transport = Transport, State = #state{transport = Transport,
socket = Socket, socket = Socket,
peername = Peername, peername = Peername,
conn_state = running, conn_state = running,
@ -149,132 +157,68 @@ init([Transport, RawSocket, Options]) ->
rate_limit = RateLimit, rate_limit = RateLimit,
pub_limit = PubLimit, pub_limit = PubLimit,
proto_state = ProtoState, proto_state = ProtoState,
parser_state = ParserState, parse_state = ParseState,
gc_state = GcState, gc_state = GcState,
enable_stats = EnableStats, enable_stats = EnableStats,
idle_timeout = IdleTimout idle_timeout = IdleTimout},
}),
ok = emqx_misc:init_proc_mng_policy(Zone), ok = emqx_misc:init_proc_mng_policy(Zone),
emqx_logger:set_metadata_peername(esockd_net:format(Peername)), gen_statem:enter_loop(?MODULE, [{hibernate_after, 2 * IdleTimout}],
gen_server:enter_loop(?MODULE, [{hibernate_after, IdleTimout}], idle, State, self(), [IdleTimout]).
State, self(), IdleTimout);
{error, Reason} ->
{stop, Reason}
end.
init_limiter(undefined) -> init_limiter(undefined) ->
undefined; undefined;
init_limiter({Rate, Burst}) -> init_limiter({Rate, Burst}) ->
esockd_rate_limit:new(Rate, Burst). esockd_rate_limit:new(Rate, Burst).
send_fun(Transport, Socket) -> callback_mode() ->
fun(Packet, Options) -> [state_functions, state_enter].
Data = emqx_frame:serialize(Packet, Options),
try Transport:async_send(Socket, Data) of
ok ->
emqx_metrics:trans(inc, 'bytes/sent', iolist_size(Data)),
ok;
Error -> Error
catch
error:Error ->
{error, Error}
end
end.
handle_call(info, _From, State) -> %%------------------------------------------------------------------------------
{reply, info(State), State}; %% Idle state
handle_call(attrs, _From, State) -> idle(enter, _, State) ->
{reply, attrs(State), State}; ok = activate_socket(State),
keep_state_and_data;
handle_call(stats, _From, State) -> idle(timeout, _Timeout, State) ->
{reply, stats(State), State}; {stop, idle_timeout, State};
handle_call(kick, _From, State) -> idle(cast, {incoming, Packet}, State) ->
{stop, {shutdown, kicked}, ok, State}; handle_packet(Packet, fun(NState) ->
{next_state, connected, NState}
end, State);
handle_call(session, _From, State = #state{proto_state = ProtoState}) -> idle(EventType, Content, State) ->
{reply, emqx_protocol:session(ProtoState), State}; ?HANDLE(EventType, Content, State).
handle_call(Req, _From, State) -> %%------------------------------------------------------------------------------
?LOG(error, "unexpected call: ~p", [Req]), %% Connected state
{reply, ignored, State}.
handle_cast(Msg, State) -> connected(enter, _, _State) ->
?LOG(error, "unexpected cast: ~p", [Msg]), %% What to do?
{noreply, State}. keep_state_and_data;
handle_info({deliver, PubOrAck}, State = #state{proto_state = ProtoState}) -> %% Handle Input
connected(cast, {incoming, Packet = ?PACKET(Type)}, State) ->
_ = emqx_metrics:received(Packet),
(Type == ?PUBLISH) andalso emqx_pd:update_counter(incoming_pubs, 1),
handle_packet(Packet, fun(NState) ->
{keep_state, NState}
end, State);
%% Handle Output
connected(info, {deliver, PubOrAck}, State = #state{proto_state = ProtoState}) ->
case emqx_protocol:deliver(PubOrAck, ProtoState) of case emqx_protocol:deliver(PubOrAck, ProtoState) of
{ok, ProtoState1} -> {ok, NProtoState} ->
State1 = State#state{proto_state = ProtoState1}, NState = State#state{proto_state = NProtoState},
{noreply, maybe_gc(PubOrAck, ensure_stats_timer(State1))}; {keep_state, maybe_gc(PubOrAck, NState)};
{error, Reason} -> {error, Reason} ->
shutdown(Reason, State) shutdown(Reason, State)
end; end;
handle_info({timeout, Timer, emit_stats}, %% Start Keepalive
State = #state{stats_timer = Timer, connected(info, {keepalive, start, Interval},
proto_state = ProtoState, State = #state{transport = Transport, socket = Socket}) ->
gc_state = GcState}) ->
emqx_metrics:commit(),
emqx_cm:set_conn_stats(emqx_protocol:client_id(ProtoState), stats(State)),
NewState = State#state{stats_timer = undefined},
Limits = erlang:get(force_shutdown_policy),
case emqx_misc:conn_proc_mng_policy(Limits) of
continue ->
{noreply, NewState};
hibernate ->
%% going to hibernate, reset gc stats
GcState1 = emqx_gc:reset(GcState),
{noreply, NewState#state{gc_state = GcState1}, hibernate};
{shutdown, Reason} ->
?LOG(warning, "shutdown due to ~p", [Reason]),
shutdown(Reason, NewState)
end;
handle_info(timeout, State) ->
shutdown(idle_timeout, State);
handle_info({shutdown, Reason}, State) ->
shutdown(Reason, State);
handle_info({shutdown, discard, {ClientId, ByPid}}, State) ->
?LOG(warning, "discarded by ~s:~p", [ClientId, ByPid]),
shutdown(discard, State);
handle_info({shutdown, conflict, {ClientId, NewPid}}, State) ->
?LOG(warning, "clientid '~s' conflict with ~p", [ClientId, NewPid]),
shutdown(conflict, State);
handle_info({TcpOrSsL, _Sock, Data}, State) when TcpOrSsL =:= tcp; TcpOrSsL =:= ssl ->
process_incoming(Data, State);
%% Rate limit here, cool:)
handle_info({tcp_passive, _Sock}, State) ->
{noreply, run_socket(ensure_rate_limit(State))};
%% FIXME Later
handle_info({ssl_passive, _Sock}, State) ->
{noreply, run_socket(ensure_rate_limit(State))};
handle_info({Err, _Sock, Reason}, State) when Err =:= tcp_error; Err =:= ssl_error ->
shutdown(Reason, State);
handle_info({Closed, _Sock}, State) when Closed =:= tcp_closed; Closed =:= ssl_closed ->
shutdown(closed, State);
%% Rate limit timer
handle_info(activate_sock, State) ->
{noreply, run_socket(State#state{conn_state = running, limit_timer = undefined})};
handle_info({inet_reply, _Sock, ok}, State) ->
{noreply, State};
handle_info({inet_reply, _Sock, {error, Reason}}, State) ->
shutdown(Reason, State);
handle_info({keepalive, start, Interval}, State = #state{transport = Transport, socket = Socket}) ->
?LOG(debug, "Keepalive at the interval of ~p", [Interval]),
StatFun = fun() -> StatFun = fun() ->
case Transport:getstat(Socket, [recv_oct]) of case Transport:getstat(Socket, [recv_oct]) of
{ok, [{recv_oct, RecvOct}]} -> {ok, RecvOct}; {ok, [{recv_oct, RecvOct}]} -> {ok, RecvOct};
@ -283,26 +227,125 @@ handle_info({keepalive, start, Interval}, State = #state{transport = Transport,
end, end,
case emqx_keepalive:start(StatFun, Interval, {keepalive, check}) of case emqx_keepalive:start(StatFun, Interval, {keepalive, check}) of
{ok, KeepAlive} -> {ok, KeepAlive} ->
{noreply, State#state{keepalive = KeepAlive}}; {keep_state, State#state{keepalive = KeepAlive}};
{error, Error} -> {error, Error} ->
shutdown(Error, State) shutdown(Error, State)
end; end;
handle_info({keepalive, check}, State = #state{keepalive = KeepAlive}) -> %% Keepalive timer
connected(info, {keepalive, check}, State = #state{keepalive = KeepAlive}) ->
case emqx_keepalive:check(KeepAlive) of case emqx_keepalive:check(KeepAlive) of
{ok, KeepAlive1} -> {ok, KeepAlive1} ->
{noreply, State#state{keepalive = KeepAlive1}}; {keep_state, State#state{keepalive = KeepAlive1}};
{error, timeout} -> {error, timeout} ->
shutdown(keepalive_timeout, State); shutdown(keepalive_timeout, State);
{error, Error} -> {error, Error} ->
shutdown(Error, State) shutdown(Error, State)
end; end;
handle_info(Info, State) -> connected(EventType, Content, State) ->
?LOG(error, "unexpected info: ~p", [Info]), ?HANDLE(EventType, Content, State).
{noreply, State}.
terminate(Reason, #state{transport = Transport, %% Handle call
handle({call, From}, info, State) ->
reply(From, info(State), State);
handle({call, From}, attrs, State) ->
reply(From, attrs(State), State);
handle({call, From}, stats, State) ->
reply(From, stats(State), State);
handle({call, From}, kick, State) ->
ok = gen_statem:reply(From, ok),
shutdown(kicked, State);
handle({call, From}, session, State = #state{proto_state = ProtoState}) ->
reply(From, emqx_protocol:session(ProtoState), State);
handle({call, From}, Req, State) ->
?LOG(error, "unexpected call: ~p", [Req]),
reply(From, ignored, State);
%% Handle cast
handle(cast, Msg, State) ->
?LOG(error, "unexpected cast: ~p", [Msg]),
{keep_state, State};
%% Handle Incoming
handle(info, {Inet, _Sock, Data}, State) when Inet == tcp; Inet == ssl ->
Oct = iolist_size(Data),
?LOG(debug, "RECV ~p", [Data]),
emqx_pd:update_counter(incoming_bytes, Oct),
emqx_metrics:trans(inc, 'bytes/received', Oct),
NState = ensure_stats_timer(maybe_gc({1, Oct}, State)),
process_incoming(Data, [], NState);
handle(info, {Error, _Sock, Reason}, State)
when Error == tcp_error; Error == ssl_error ->
shutdown(Reason, State);
handle(info, {Closed, _Sock}, State)
when Closed == tcp_closed; Closed == ssl_closed ->
shutdown(closed, State);
handle(info, {tcp_passive, _Sock}, State) ->
%% Rate limit here:)
NState = ensure_rate_limit(State),
ok = activate_socket(NState),
{keep_state, NState};
handle(info, activate_socket, State) ->
%% Rate limit timer expired.
ok = activate_socket(State),
{keep_state, State#state{conn_state = running, limit_timer = undefined}};
handle(info, {inet_reply, _Sock, ok}, State) ->
%% something sent
{keep_state, ensure_stats_timer(State)};
handle(info, {inet_reply, _Sock, {error, Reason}}, State) ->
shutdown(Reason, State);
handle(info, {timeout, Timer, emit_stats},
State = #state{stats_timer = Timer,
proto_state = ProtoState,
gc_state = GcState}) ->
emqx_metrics:commit(),
emqx_cm:set_conn_stats(emqx_protocol:client_id(ProtoState), stats(State)),
NState = State#state{stats_timer = undefined},
Limits = erlang:get(force_shutdown_policy),
case emqx_misc:conn_proc_mng_policy(Limits) of
continue ->
{keep_state, NState};
hibernate ->
%% going to hibernate, reset gc stats
GcState1 = emqx_gc:reset(GcState),
{keep_state, NState#state{gc_state = GcState1}, hibernate};
{shutdown, Reason} ->
?LOG(warning, "shutdown due to ~p", [Reason]),
shutdown(Reason, NState)
end;
handle(info, {shutdown, discard, {ClientId, ByPid}}, State) ->
?LOG(warning, "discarded by ~s:~p", [ClientId, ByPid]),
shutdown(discard, State);
handle(info, {shutdown, conflict, {ClientId, NewPid}}, State) ->
?LOG(warning, "clientid '~s' conflict with ~p", [ClientId, NewPid]),
shutdown(conflict, State);
handle(info, {shutdown, Reason}, State) ->
shutdown(Reason, State);
handle(info, Info, State) ->
?LOG(error, "unexpected info: ~p", [Info]),
{keep_state, State}.
code_change(_Vsn, State, Data, _Extra) ->
{ok, State, Data}.
terminate(Reason, _StateName, #state{transport = Transport,
socket = Socket, socket = Socket,
keepalive = KeepAlive, keepalive = KeepAlive,
proto_state = ProtoState}) -> proto_state = ProtoState}) ->
@ -312,65 +355,55 @@ terminate(Reason, #state{transport = Transport,
case {ProtoState, Reason} of case {ProtoState, Reason} of
{undefined, _} -> ok; {undefined, _} -> ok;
{_, {shutdown, Error}} -> {_, {shutdown, Error}} ->
emqx_protocol:shutdown(Error, ProtoState); emqx_protocol:terminate(Error, ProtoState);
{_, Reason} -> {_, Reason} ->
emqx_protocol:shutdown(Reason, ProtoState) emqx_protocol:terminate(Reason, ProtoState)
end. end.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%------------------------------------------------------------------------------
%% Internals: process incoming, parse and handle packets
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Process incoming data
process_incoming(Data, State) -> process_incoming(<<>>, Packets, State) ->
Oct = iolist_size(Data), {keep_state, State, next_events(Packets)};
?LOG(debug, "RECV ~p", [Data]),
emqx_pd:update_counter(incoming_bytes, Oct),
emqx_metrics:trans(inc, 'bytes/received', Oct),
case handle_packet(Data, State) of
{noreply, State1} ->
State2 = maybe_gc({1, Oct}, State1),
{noreply, ensure_stats_timer(State2)};
Shutdown -> Shutdown
end.
%% Parse and handle packets process_incoming(Data, Packets, State = #state{parse_state = ParseState}) ->
handle_packet(<<>>, State) -> try emqx_frame:parse(Data, ParseState) of
{noreply, State}; {ok, Packet, Rest} ->
process_incoming(Rest, [Packet|Packets], reset_parser(State));
handle_packet(Data, State = #state{proto_state = ProtoState, {more, NewParseState} ->
parser_state = ParserState, {keep_state, State#state{parse_state = NewParseState}, next_events(Packets)};
idle_timeout = IdleTimeout}) ->
try emqx_frame:parse(Data, ParserState) of
{more, ParserState1} ->
{noreply, State#state{parser_state = ParserState1}, IdleTimeout};
{ok, Packet = ?PACKET(Type), Rest} ->
emqx_metrics:received(Packet),
(Type == ?PUBLISH) andalso emqx_pd:update_counter(incoming_pubs, 1),
case emqx_protocol:received(Packet, ProtoState) of
{ok, ProtoState1} ->
handle_packet(Rest, reset_parser(State#state{proto_state = ProtoState1}));
{error, Reason} -> {error, Reason} ->
?LOG(error, "Process packet error - ~p", [Reason]),
shutdown(Reason, State);
{error, Reason, ProtoState1} ->
shutdown(Reason, State#state{proto_state = ProtoState1});
{stop, Error, ProtoState1} ->
stop(Error, State#state{proto_state = ProtoState1})
end;
{error, Reason} ->
?LOG(error, "Parse frame error - ~p", [Reason]),
shutdown(Reason, State) shutdown(Reason, State)
catch catch
_:Error -> _:Error:Stk->
?LOG(error, "Parse failed for ~p~nError data:~p", [Error, Data]), ?LOG(error, "Parse failed for ~p~nStacktrace:~p~nError data:~p", [Error, Stk, Data]),
shutdown(parse_error, State) shutdown(Error, State)
end. end.
reset_parser(State = #state{proto_state = ProtoState}) -> reset_parser(State = #state{proto_state = ProtoState}) ->
State#state{parser_state = emqx_protocol:parser(ProtoState)}. State#state{parse_state = emqx_protocol:parser(ProtoState)}.
next_events([]) ->
[];
next_events([Packet]) ->
{next_event, cast, {incoming, Packet}};
next_events(Packets) ->
[next_events([Packet]) || Packet <- lists:reverse(Packets)].
%%------------------------------------------------------------------------------
%% Handle incoming packet
handle_packet(Packet, SuccFun, State = #state{proto_state = ProtoState}) ->
case emqx_protocol:received(Packet, ProtoState) of
{ok, NProtoState} ->
SuccFun(State#state{proto_state = NProtoState});
{error, Reason} ->
shutdown(Reason, State);
{error, Reason, NProtoState} ->
shutdown(Reason, State#state{proto_state = NProtoState});
{stop, Error, NProtoState} ->
stop(Error, State#state{proto_state = NProtoState})
end.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Ensure rate limit %% Ensure rate limit
@ -389,27 +422,27 @@ ensure_rate_limit([{Rl, Pos, Cnt}|Limiters], State) ->
{0, Rl1} -> {0, Rl1} ->
ensure_rate_limit(Limiters, setelement(Pos, State, Rl1)); ensure_rate_limit(Limiters, setelement(Pos, State, Rl1));
{Pause, Rl1} -> {Pause, Rl1} ->
TRef = erlang:send_after(Pause, self(), activate_sock), TRef = erlang:send_after(Pause, self(), activate_socket),
setelement(Pos, State#state{conn_state = blocked, limit_timer = TRef}, Rl1) setelement(Pos, State#state{conn_state = blocked, limit_timer = TRef}, Rl1)
end. end.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Activate socket %% Activate socket
run_socket(State = #state{conn_state = blocked}) -> activate_socket(#state{conn_state = blocked}) ->
State; ok;
run_socket(State = #state{transport = Transport, socket = Socket, active_n = N}) -> activate_socket(#state{transport = Transport, socket = Socket, active_n = N}) ->
TrueOrN = case Transport:is_ssl(Socket) of TrueOrN = case Transport:is_ssl(Socket) of
true -> true; %% Cannot set '{active, N}' for SSL:( true -> true; %% Cannot set '{active, N}' for SSL:(
false -> N false -> N
end, end,
ensure_ok_or_exit(Transport:setopts(Socket, [{active, TrueOrN}])), case Transport:setopts(Socket, [{active, TrueOrN}]) of
State. ok -> ok;
{error, Reason} ->
ensure_ok_or_exit(ok) -> ok; self() ! {shutdown, Reason},
ensure_ok_or_exit({error, Reason}) -> ok
self() ! {shutdown, Reason}. end.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Ensure stats timer %% Ensure stats timer
@ -418,6 +451,7 @@ ensure_stats_timer(State = #state{enable_stats = true,
stats_timer = undefined, stats_timer = undefined,
idle_timeout = IdleTimeout}) -> idle_timeout = IdleTimeout}) ->
State#state{stats_timer = emqx_misc:start_timer(IdleTimeout, emit_stats)}; State#state{stats_timer = emqx_misc:start_timer(IdleTimeout, emit_stats)};
ensure_stats_timer(State) -> State. ensure_stats_timer(State) -> State.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -425,20 +459,28 @@ ensure_stats_timer(State) -> State.
maybe_gc(_, State = #state{gc_state = undefined}) -> maybe_gc(_, State = #state{gc_state = undefined}) ->
State; State;
maybe_gc({publish, _PacketId, #message{payload = Payload}}, State) -> maybe_gc({publish, _, #message{payload = Payload}}, State) ->
Oct = iolist_size(Payload), Oct = iolist_size(Payload),
maybe_gc({1, Oct}, State); maybe_gc({1, Oct}, State);
maybe_gc(Packets, State) when is_list(Packets) ->
{Cnt, Oct} =
lists:unzip([{1, iolist_size(Payload)}
|| {publish, _, #message{payload = Payload}} <- Packets]),
maybe_gc({lists:sum(Cnt), lists:sum(Oct)}, State);
maybe_gc({Cnt, Oct}, State = #state{gc_state = GCSt}) -> maybe_gc({Cnt, Oct}, State = #state{gc_state = GCSt}) ->
{_, GCSt1} = emqx_gc:run(Cnt, Oct, GCSt), {_, GCSt1} = emqx_gc:run(Cnt, Oct, GCSt),
State#state{gc_state = GCSt1}; State#state{gc_state = GCSt1};
maybe_gc(_, State) -> maybe_gc(_, State) -> State.
State.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Shutdown or stop %% Helper functions
reply(From, Reply, State) ->
{keep_state, State, [{reply, From, Reply}]}.
shutdown(Reason, State) -> shutdown(Reason, State) ->
stop({shutdown, Reason}, State). stop({shutdown, Reason}, State).
stop(Reason, State) -> stop(Reason, State) ->
{stop, Reason, State}. {stop, Reason, State}.

View File

@ -29,10 +29,10 @@
-export([parser/1]). -export([parser/1]).
-export([session/1]). -export([session/1]).
-export([received/2]). -export([received/2]).
-export([process_packet/2]). -export([process/2]).
-export([deliver/2]). -export([deliver/2]).
-export([send/2]). -export([send/2]).
-export([shutdown/2]). -export([terminate/2]).
-export_type([state/0]). -export_type([state/0]).
@ -53,6 +53,8 @@
clean_start, clean_start,
topic_aliases, topic_aliases,
packet_size, packet_size,
will_topic,
will_msg,
keepalive, keepalive,
mountpoint, mountpoint,
is_super, is_super,
@ -130,11 +132,13 @@ info(PState = #pstate{conn_props = ConnProps,
ack_props = AckProps, ack_props = AckProps,
session = Session, session = Session,
topic_aliases = Aliases, topic_aliases = Aliases,
will_msg = WillMsg,
enable_acl = EnableAcl}) -> enable_acl = EnableAcl}) ->
attrs(PState) ++ [{conn_props, ConnProps}, attrs(PState) ++ [{conn_props, ConnProps},
{ack_props, AckProps}, {ack_props, AckProps},
{session, Session}, {session, Session},
{topic_aliases, Aliases}, {topic_aliases, Aliases},
{will_msg, WillMsg},
{enable_acl, EnableAcl}]. {enable_acl, EnableAcl}].
attrs(#pstate{zone = Zone, attrs(#pstate{zone = Zone,
@ -218,15 +222,16 @@ parser(#pstate{packet_size = Size, proto_ver = Ver}) ->
%% Packet Received %% Packet Received
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
set_protover(?CONNECT_PACKET(#mqtt_packet_connect{ set_protover(?CONNECT_PACKET(#mqtt_packet_connect{proto_ver = ProtoVer}), PState) ->
proto_ver = ProtoVer}),
PState) ->
PState#pstate{proto_ver = ProtoVer}; PState#pstate{proto_ver = ProtoVer};
set_protover(_Packet, PState) -> set_protover(_Packet, PState) ->
PState. PState.
-spec(received(emqx_mqtt_types:packet(), state()) -> -spec(received(emqx_mqtt_types:packet(), state())
{ok, state()} | {error, term()} | {error, term(), state()} | {stop, term(), state()}). -> {ok, state()}
| {error, term()}
| {error, term(), state()}
| {stop, term(), state()}).
received(?PACKET(Type), PState = #pstate{connected = false}) when Type =/= ?CONNECT -> received(?PACKET(Type), PState = #pstate{connected = false}) when Type =/= ?CONNECT ->
{error, proto_not_connected, PState}; {error, proto_not_connected, PState};
@ -234,15 +239,15 @@ received(?PACKET(?CONNECT), PState = #pstate{connected = true}) ->
{error, proto_unexpected_connect, PState}; {error, proto_unexpected_connect, PState};
received(Packet = ?PACKET(Type), PState) -> received(Packet = ?PACKET(Type), PState) ->
PState1 = set_protover(Packet, PState),
trace(recv, Packet), trace(recv, Packet),
PState1 = set_protover(Packet, PState),
try emqx_packet:validate(Packet) of try emqx_packet:validate(Packet) of
true -> true ->
case preprocess_properties(Packet, PState1) of case preprocess_properties(Packet, PState1) of
{ok, Packet1, PState2} ->
process(Packet1, inc_stats(recv, Type, PState2));
{error, ReasonCode} -> {error, ReasonCode} ->
{error, ReasonCode, PState1}; {error, ReasonCode, PState1}
{Packet1, PState2} ->
process_packet(Packet1, inc_stats(recv, Type, PState2))
end end
catch catch
error:protocol_error -> error:protocol_error ->
@ -268,13 +273,14 @@ received(Packet = ?PACKET(Type), PState) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Preprocess MQTT Properties %% Preprocess MQTT Properties
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
preprocess_properties(Packet = #mqtt_packet{ preprocess_properties(Packet = #mqtt_packet{
variable = #mqtt_packet_connect{ variable = #mqtt_packet_connect{
properties = #{'Topic-Alias-Maximum' := ToClient} properties = #{'Topic-Alias-Maximum' := ToClient}
} }
}, },
PState = #pstate{topic_alias_maximum = TopicAliasMaximum}) -> PState = #pstate{topic_alias_maximum = TopicAliasMaximum}) ->
{Packet, PState#pstate{topic_alias_maximum = TopicAliasMaximum#{to_client => ToClient}}}; {ok, Packet, PState#pstate{topic_alias_maximum = TopicAliasMaximum#{to_client => ToClient}}};
%% Subscription Identifier %% Subscription Identifier
preprocess_properties(Packet = #mqtt_packet{ preprocess_properties(Packet = #mqtt_packet{
@ -285,7 +291,7 @@ preprocess_properties(Packet = #mqtt_packet{
}, },
PState = #pstate{proto_ver = ?MQTT_PROTO_V5}) -> PState = #pstate{proto_ver = ?MQTT_PROTO_V5}) ->
TopicFilters1 = [{Topic, SubOpts#{subid => SubId}} || {Topic, SubOpts} <- TopicFilters], TopicFilters1 = [{Topic, SubOpts#{subid => SubId}} || {Topic, SubOpts} <- TopicFilters],
{Packet#mqtt_packet{variable = Subscribe#mqtt_packet_subscribe{topic_filters = TopicFilters1}}, PState}; {ok, Packet#mqtt_packet{variable = Subscribe#mqtt_packet_subscribe{topic_filters = TopicFilters1}}, PState};
%% Topic Alias Mapping %% Topic Alias Mapping
preprocess_properties(#mqtt_packet{ preprocess_properties(#mqtt_packet{
@ -306,7 +312,7 @@ preprocess_properties(Packet = #mqtt_packet{
topic_alias_maximum = #{from_client := TopicAliasMaximum}}) -> topic_alias_maximum = #{from_client := TopicAliasMaximum}}) ->
case AliasId =< TopicAliasMaximum of case AliasId =< TopicAliasMaximum of
true -> true ->
{Packet#mqtt_packet{variable = Publish#mqtt_packet_publish{ {ok, Packet#mqtt_packet{variable = Publish#mqtt_packet_publish{
topic_name = maps:get(AliasId, Aliases, <<>>)}}, PState}; topic_name = maps:get(AliasId, Aliases, <<>>)}}, PState};
false -> false ->
deliver({disconnect, ?RC_TOPIC_ALIAS_INVALID}, PState), deliver({disconnect, ?RC_TOPIC_ALIAS_INVALID}, PState),
@ -323,19 +329,19 @@ preprocess_properties(Packet = #mqtt_packet{
topic_alias_maximum = #{from_client := TopicAliasMaximum}}) -> topic_alias_maximum = #{from_client := TopicAliasMaximum}}) ->
case AliasId =< TopicAliasMaximum of case AliasId =< TopicAliasMaximum of
true -> true ->
{Packet, PState#pstate{topic_aliases = maps:put(AliasId, Topic, Aliases)}}; {ok, Packet, PState#pstate{topic_aliases = maps:put(AliasId, Topic, Aliases)}};
false -> false ->
deliver({disconnect, ?RC_TOPIC_ALIAS_INVALID}, PState), deliver({disconnect, ?RC_TOPIC_ALIAS_INVALID}, PState),
{error, ?RC_TOPIC_ALIAS_INVALID} {error, ?RC_TOPIC_ALIAS_INVALID}
end; end;
preprocess_properties(Packet, PState) -> preprocess_properties(Packet, PState) ->
{Packet, PState}. {ok, Packet, PState}.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Process MQTT Packet %% Process MQTT Packet
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
process_packet(?CONNECT_PACKET( process(?CONNECT_PACKET(
#mqtt_packet_connect{proto_name = ProtoName, #mqtt_packet_connect{proto_name = ProtoName,
proto_ver = ProtoVer, proto_ver = ProtoVer,
is_bridge = IsBridge, is_bridge = IsBridge,
@ -394,7 +400,7 @@ process_packet(?CONNECT_PACKET(
{ReasonCode, PState1} {ReasonCode, PState1}
end); end);
process_packet(Packet = ?PUBLISH_PACKET(?QOS_0, Topic, _PacketId, _Payload), PState) -> process(Packet = ?PUBLISH_PACKET(?QOS_0, Topic, _PacketId, _Payload), PState) ->
case check_publish(Packet, PState) of case check_publish(Packet, PState) of
{ok, PState1} -> {ok, PState1} ->
do_publish(Packet, PState1); do_publish(Packet, PState1);
@ -404,7 +410,7 @@ process_packet(Packet = ?PUBLISH_PACKET(?QOS_0, Topic, _PacketId, _Payload), PSt
do_acl_deny_action(Packet, ReasonCode, PState) do_acl_deny_action(Packet, ReasonCode, PState)
end; end;
process_packet(Packet = ?PUBLISH_PACKET(?QOS_1, Topic, PacketId, _Payload), PState) -> process(Packet = ?PUBLISH_PACKET(?QOS_1, Topic, PacketId, _Payload), PState) ->
case check_publish(Packet, PState) of case check_publish(Packet, PState) of
{ok, PState1} -> {ok, PState1} ->
do_publish(Packet, PState1); do_publish(Packet, PState1);
@ -414,12 +420,11 @@ process_packet(Packet = ?PUBLISH_PACKET(?QOS_1, Topic, PacketId, _Payload), PSta
case deliver({puback, PacketId, ReasonCode}, PState) of case deliver({puback, PacketId, ReasonCode}, PState) of
{ok, PState1} -> {ok, PState1} ->
do_acl_deny_action(Packet, ReasonCode, PState1); do_acl_deny_action(Packet, ReasonCode, PState1);
Error -> Error -> Error
Error
end end
end; end;
process_packet(Packet = ?PUBLISH_PACKET(?QOS_2, Topic, PacketId, _Payload), PState) -> process(Packet = ?PUBLISH_PACKET(?QOS_2, Topic, PacketId, _Payload), PState) ->
case check_publish(Packet, PState) of case check_publish(Packet, PState) of
{ok, PState1} -> {ok, PState1} ->
do_publish(Packet, PState1); do_publish(Packet, PState1);
@ -429,15 +434,14 @@ process_packet(Packet = ?PUBLISH_PACKET(?QOS_2, Topic, PacketId, _Payload), PSta
case deliver({pubrec, PacketId, ReasonCode}, PState) of case deliver({pubrec, PacketId, ReasonCode}, PState) of
{ok, PState1} -> {ok, PState1} ->
do_acl_deny_action(Packet, ReasonCode, PState1); do_acl_deny_action(Packet, ReasonCode, PState1);
Error -> Error -> Error
Error
end end
end; end;
process_packet(?PUBACK_PACKET(PacketId, ReasonCode), PState = #pstate{session = SPid}) -> process(?PUBACK_PACKET(PacketId, ReasonCode), PState = #pstate{session = SPid}) ->
{ok = emqx_session:puback(SPid, PacketId, ReasonCode), PState}; {ok = emqx_session:puback(SPid, PacketId, ReasonCode), PState};
process_packet(?PUBREC_PACKET(PacketId, ReasonCode), PState = #pstate{session = SPid}) -> process(?PUBREC_PACKET(PacketId, ReasonCode), PState = #pstate{session = SPid}) ->
case emqx_session:pubrec(SPid, PacketId, ReasonCode) of case emqx_session:pubrec(SPid, PacketId, ReasonCode) of
ok -> ok ->
send(?PUBREL_PACKET(PacketId), PState); send(?PUBREL_PACKET(PacketId), PState);
@ -445,7 +449,7 @@ process_packet(?PUBREC_PACKET(PacketId, ReasonCode), PState = #pstate{session =
send(?PUBREL_PACKET(PacketId, NotFound), PState) send(?PUBREL_PACKET(PacketId, NotFound), PState)
end; end;
process_packet(?PUBREL_PACKET(PacketId, ReasonCode), PState = #pstate{session = SPid}) -> process(?PUBREL_PACKET(PacketId, ReasonCode), PState = #pstate{session = SPid}) ->
case emqx_session:pubrel(SPid, PacketId, ReasonCode) of case emqx_session:pubrel(SPid, PacketId, ReasonCode) of
ok -> ok ->
send(?PUBCOMP_PACKET(PacketId), PState); send(?PUBCOMP_PACKET(PacketId), PState);
@ -453,10 +457,10 @@ process_packet(?PUBREL_PACKET(PacketId, ReasonCode), PState = #pstate{session =
send(?PUBCOMP_PACKET(PacketId, NotFound), PState) send(?PUBCOMP_PACKET(PacketId, NotFound), PState)
end; end;
process_packet(?PUBCOMP_PACKET(PacketId, ReasonCode), PState = #pstate{session = SPid}) -> process(?PUBCOMP_PACKET(PacketId, ReasonCode), PState = #pstate{session = SPid}) ->
{ok = emqx_session:pubcomp(SPid, PacketId, ReasonCode), PState}; {ok = emqx_session:pubcomp(SPid, PacketId, ReasonCode), PState};
process_packet(Packet = ?SUBSCRIBE_PACKET(PacketId, Properties, RawTopicFilters), process(Packet = ?SUBSCRIBE_PACKET(PacketId, Properties, RawTopicFilters),
PState = #pstate{session = SPid, mountpoint = Mountpoint, PState = #pstate{session = SPid, mountpoint = Mountpoint,
proto_ver = ProtoVer, is_bridge = IsBridge, proto_ver = ProtoVer, is_bridge = IsBridge,
ignore_loop = IgnoreLoop}) -> ignore_loop = IgnoreLoop}) ->
@ -483,13 +487,12 @@ process_packet(Packet = ?SUBSCRIBE_PACKET(PacketId, Properties, RawTopicFilters)
deliver({suback, PacketId, ReasonCodes}, PState) deliver({suback, PacketId, ReasonCodes}, PState)
end; end;
{error, TopicFilters} -> {error, TopicFilters} ->
{ReverseSubTopics, ReverseReasonCodes} = {SubTopics, ReasonCodes} =
lists:foldl(fun({Topic, #{rc := ?RC_SUCCESS}}, {Topics, Codes}) -> lists:foldr(fun({Topic, #{rc := ?RC_SUCCESS}}, {Topics, Codes}) ->
{[Topic|Topics], [?RC_IMPLEMENTATION_SPECIFIC_ERROR | Codes]}; {[Topic|Topics], [?RC_IMPLEMENTATION_SPECIFIC_ERROR | Codes]};
({Topic, #{rc := Code}}, {Topics, Codes}) -> ({Topic, #{rc := Code}}, {Topics, Codes}) ->
{[Topic|Topics], [Code|Codes]} {[Topic|Topics], [Code|Codes]}
end, {[], []}, TopicFilters), end, {[], []}, TopicFilters),
{SubTopics, ReasonCodes} = {lists:reverse(ReverseSubTopics), lists:reverse(ReverseReasonCodes)},
?LOG(warning, "Cannot subscribe ~p for ~p", ?LOG(warning, "Cannot subscribe ~p for ~p",
[SubTopics, [emqx_reason_codes:text(R) || R <- ReasonCodes]]), [SubTopics, [emqx_reason_codes:text(R) || R <- ReasonCodes]]),
case deliver({suback, PacketId, ReasonCodes}, PState) of case deliver({suback, PacketId, ReasonCodes}, PState) of
@ -500,7 +503,7 @@ process_packet(Packet = ?SUBSCRIBE_PACKET(PacketId, Properties, RawTopicFilters)
end end
end; end;
process_packet(?UNSUBSCRIBE_PACKET(PacketId, Properties, RawTopicFilters), process(?UNSUBSCRIBE_PACKET(PacketId, Properties, RawTopicFilters),
PState = #pstate{session = SPid, mountpoint = MountPoint}) -> PState = #pstate{session = SPid, mountpoint = MountPoint}) ->
case emqx_hooks:run('client.unsubscribe', [credentials(PState)], case emqx_hooks:run('client.unsubscribe', [credentials(PState)],
parse_topic_filters(?UNSUBSCRIBE, RawTopicFilters)) of parse_topic_filters(?UNSUBSCRIBE, RawTopicFilters)) of
@ -514,22 +517,25 @@ process_packet(?UNSUBSCRIBE_PACKET(PacketId, Properties, RawTopicFilters),
deliver({unsuback, PacketId, ReasonCodes}, PState) deliver({unsuback, PacketId, ReasonCodes}, PState)
end; end;
process_packet(?PACKET(?PINGREQ), PState) -> process(?PACKET(?PINGREQ), PState) ->
send(?PACKET(?PINGRESP), PState); send(?PACKET(?PINGRESP), PState);
process_packet(?DISCONNECT_PACKET(?RC_SUCCESS, #{'Session-Expiry-Interval' := Interval}), process(?DISCONNECT_PACKET(?RC_SUCCESS, #{'Session-Expiry-Interval' := Interval}),
PState = #pstate{session = SPid, conn_props = #{'Session-Expiry-Interval' := OldInterval}}) -> PState = #pstate{session = SPid, conn_props = #{'Session-Expiry-Interval' := OldInterval}}) ->
case Interval =/= 0 andalso OldInterval =:= 0 of case Interval =/= 0 andalso OldInterval =:= 0 of
true -> true ->
deliver({disconnect, ?RC_PROTOCOL_ERROR}, PState), deliver({disconnect, ?RC_PROTOCOL_ERROR}, PState),
{error, protocol_error, PState}; {error, protocol_error, PState#pstate{will_msg = undefined}};
false -> false ->
emqx_session:update_expiry_interval(SPid, Interval), emqx_session:update_expiry_interval(SPid, Interval),
{stop, normal, PState} %% Clean willmsg
{stop, normal, PState#pstate{will_msg = undefined}}
end; end;
process_packet(?DISCONNECT_PACKET(?RC_SUCCESS), PState) ->
{stop, normal, PState}; process(?DISCONNECT_PACKET(?RC_SUCCESS), PState) ->
process_packet(?DISCONNECT_PACKET(_), PState) -> {stop, normal, PState#pstate{will_msg = undefined}};
process(?DISCONNECT_PACKET(_), PState) ->
{stop, {shutdown, abnormal_disconnet}, PState}. {stop, {shutdown, abnormal_disconnet}, PState}.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -562,15 +568,16 @@ do_publish(Packet = ?PUBLISH_PACKET(QoS, PacketId),
puback(?QOS_0, _PacketId, _Result, PState) -> puback(?QOS_0, _PacketId, _Result, PState) ->
{ok, PState}; {ok, PState};
puback(?QOS_1, PacketId, [], PState) -> puback(?QOS_1, PacketId, {ok, []}, PState) ->
deliver({puback, PacketId, ?RC_NO_MATCHING_SUBSCRIBERS}, PState); deliver({puback, PacketId, ?RC_NO_MATCHING_SUBSCRIBERS}, PState);
puback(?QOS_1, PacketId, [_|_], PState) -> %%TODO: check the dispatch? %%TODO: calc the deliver count?
puback(?QOS_1, PacketId, {ok, _Result}, PState) ->
deliver({puback, PacketId, ?RC_SUCCESS}, PState); deliver({puback, PacketId, ?RC_SUCCESS}, PState);
puback(?QOS_1, PacketId, {error, ReasonCode}, PState) -> puback(?QOS_1, PacketId, {error, ReasonCode}, PState) ->
deliver({puback, PacketId, ReasonCode}, PState); deliver({puback, PacketId, ReasonCode}, PState);
puback(?QOS_2, PacketId, [], PState) -> puback(?QOS_2, PacketId, {ok, []}, PState) ->
deliver({pubrec, PacketId, ?RC_NO_MATCHING_SUBSCRIBERS}, PState); deliver({pubrec, PacketId, ?RC_NO_MATCHING_SUBSCRIBERS}, PState);
puback(?QOS_2, PacketId, [_|_], PState) -> %%TODO: check the dispatch? puback(?QOS_2, PacketId, {ok, _Result}, PState) ->
deliver({pubrec, PacketId, ?RC_SUCCESS}, PState); deliver({pubrec, PacketId, ?RC_SUCCESS}, PState);
puback(?QOS_2, PacketId, {error, ReasonCode}, PState) -> puback(?QOS_2, PacketId, {error, ReasonCode}, PState) ->
deliver({pubrec, PacketId, ReasonCode}, PState). deliver({pubrec, PacketId, ReasonCode}, PState).
@ -579,7 +586,17 @@ puback(?QOS_2, PacketId, {error, ReasonCode}, PState) ->
%% Deliver Packet -> Client %% Deliver Packet -> Client
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-spec(deliver(tuple(), state()) -> {ok, state()} | {error, term()}). -spec(deliver(list(tuple()) | tuple(), state()) -> {ok, state()} | {error, term()}).
deliver([], PState) ->
{ok, PState};
deliver([Pub|More], PState) ->
case deliver(Pub, PState) of
{ok, PState1} ->
deliver(More, PState1);
{error, _} = Error ->
Error
end;
deliver({connack, ReasonCode}, PState) -> deliver({connack, ReasonCode}, PState) ->
send(?CONNACK_PACKET(ReasonCode), PState); send(?CONNACK_PACKET(ReasonCode), PState);
@ -666,11 +683,13 @@ deliver({disconnect, _ReasonCode}, PState) ->
%% Send Packet to Client %% Send Packet to Client
-spec(send(emqx_mqtt_types:packet(), state()) -> {ok, state()} | {error, term()}). -spec(send(emqx_mqtt_types:packet(), state()) -> {ok, state()} | {error, term()}).
send(Packet = ?PACKET(Type), PState = #pstate{proto_ver = Ver, sendfun = SendFun}) -> send(Packet = ?PACKET(Type), PState = #pstate{proto_ver = Ver, sendfun = Send}) ->
trace(send, Packet), Data = emqx_frame:serialize(Packet, #{version => Ver}),
case SendFun(Packet, #{version => Ver}) of case Send(Data) of
ok -> ok ->
trace(send, Packet),
emqx_metrics:sent(Packet), emqx_metrics:sent(Packet),
emqx_metrics:trans(inc, 'bytes/sent', iolist_size(Data)),
{ok, inc_stats(send, Type, PState)}; {ok, inc_stats(send, Type, PState)};
{error, Reason} -> {error, Reason} ->
{error, Reason} {error, Reason}
@ -809,14 +828,13 @@ check_will_topic(#mqtt_packet_connect{will_topic = WillTopic} = ConnPkt, PState)
{error, ?RC_TOPIC_NAME_INVALID} {error, ?RC_TOPIC_NAME_INVALID}
end. end.
check_will_acl(_ConnPkt, #pstate{enable_acl = EnableAcl}) check_will_acl(_ConnPkt, #pstate{enable_acl = EnableAcl}) when not EnableAcl ->
when not EnableAcl ->
ok; ok;
check_will_acl(#mqtt_packet_connect{will_topic = WillTopic}, PState) -> check_will_acl(#mqtt_packet_connect{will_topic = WillTopic}, PState) ->
case emqx_access_control:check_acl(credentials(PState), publish, WillTopic) of case emqx_access_control:check_acl(credentials(PState), publish, WillTopic) of
allow -> ok; allow -> ok;
deny -> deny ->
?LOG(warning, "Will message (to ~s) validation failed, acl denied", [WillTopic]), ?LOG(warning, "Cannot publish will message to ~p for acl denied", [WillTopic]),
{error, ?RC_NOT_AUTHORIZED} {error, ?RC_NOT_AUTHORIZED}
end. end.
@ -892,15 +910,15 @@ inc_stats(Type, Stats = #{pkt := PktCnt, msg := MsgCnt}) ->
false -> MsgCnt false -> MsgCnt
end}. end}.
shutdown(_Reason, #pstate{client_id = undefined}) -> terminate(_Reason, #pstate{client_id = undefined}) ->
ok; ok;
shutdown(_Reason, #pstate{connected = false}) -> terminate(_Reason, #pstate{connected = false}) ->
ok; ok;
shutdown(conflict, _PState) -> terminate(conflict, _PState) ->
ok; ok;
shutdown(discard, _PState) -> terminate(discard, _PState) ->
ok; ok;
shutdown(Reason, PState) -> terminate(Reason, PState) ->
?LOG(info, "Shutdown for ~p", [Reason]), ?LOG(info, "Shutdown for ~p", [Reason]),
emqx_hooks:run('client.disconnected', [credentials(PState), Reason]). emqx_hooks:run('client.disconnected', [credentials(PState), Reason]).

View File

@ -71,8 +71,11 @@
%% Clean Start Flag %% Clean Start Flag
clean_start = false :: boolean(), clean_start = false :: boolean(),
%% Client Binding: local | remote %% Conn Binding: local | remote
binding = local :: local | remote, %% binding = local :: local | remote,
%% Deliver fun
deliver_fun :: function(),
%% ClientId: Identifier of Session %% ClientId: Identifier of Session
client_id :: binary(), client_id :: binary(),
@ -157,6 +160,8 @@
-export_type([attr/0]). -export_type([attr/0]).
-define(DEFAULT_BATCH_N, 1000).
%% @doc Start a session proc. %% @doc Start a session proc.
-spec(start_link(SessAttrs :: map()) -> {ok, pid()}). -spec(start_link(SessAttrs :: map()) -> {ok, pid()}).
start_link(SessAttrs) -> start_link(SessAttrs) ->
@ -196,13 +201,13 @@ attrs(SPid) when is_pid(SPid) ->
gen_server:call(SPid, attrs, infinity); gen_server:call(SPid, attrs, infinity);
attrs(#state{clean_start = CleanStart, attrs(#state{clean_start = CleanStart,
binding = Binding,
client_id = ClientId, client_id = ClientId,
conn_pid = ConnPid,
username = Username, username = Username,
expiry_interval = ExpiryInterval, expiry_interval = ExpiryInterval,
created_at = CreatedAt}) -> created_at = CreatedAt}) ->
[{clean_start, CleanStart}, [{clean_start, CleanStart},
{binding, Binding}, {binding, binding(ConnPid)},
{client_id, ClientId}, {client_id, ClientId},
{username, Username}, {username, Username},
{expiry_interval, ExpiryInterval div 1000}, {expiry_interval, ExpiryInterval div 1000},
@ -249,19 +254,19 @@ subscribe(SPid, PacketId, Properties, TopicFilters) ->
%% @doc Called by connection processes when publishing messages %% @doc Called by connection processes when publishing messages
-spec(publish(spid(), emqx_mqtt_types:packet_id(), emqx_types:message()) -spec(publish(spid(), emqx_mqtt_types:packet_id(), emqx_types:message())
-> emqx_types:deliver_results() | {error, term()}). -> {ok, emqx_types:deliver_results()} | {error, term()}).
publish(_SPid, _PacketId, Msg = #message{qos = ?QOS_0}) -> publish(_SPid, _PacketId, Msg = #message{qos = ?QOS_0}) ->
%% Publish QoS0 message directly %% Publish QoS0 message directly
emqx_broker:publish(Msg); {ok, emqx_broker:publish(Msg)};
publish(_SPid, _PacketId, Msg = #message{qos = ?QOS_1}) -> publish(_SPid, _PacketId, Msg = #message{qos = ?QOS_1}) ->
%% Publish QoS1 message directly %% Publish QoS1 message directly
emqx_broker:publish(Msg); {ok, emqx_broker:publish(Msg)};
publish(SPid, PacketId, Msg = #message{qos = ?QOS_2, timestamp = Ts}) -> publish(SPid, PacketId, Msg = #message{qos = ?QOS_2, timestamp = Ts}) ->
%% Register QoS2 message packet ID (and timestamp) to session, then publish %% Register QoS2 message packet ID (and timestamp) to session, then publish
case gen_server:call(SPid, {register_publish_packet_id, PacketId, Ts}, infinity) of case gen_server:call(SPid, {register_publish_packet_id, PacketId, Ts}, infinity) of
ok -> emqx_broker:publish(Msg); ok -> {ok, emqx_broker:publish(Msg)};
{error, Reason} -> {error, Reason} {error, Reason} -> {error, Reason}
end. end.
@ -342,7 +347,7 @@ init([Parent, #{zone := Zone,
IdleTimout = get_env(Zone, idle_timeout, 30000), IdleTimout = get_env(Zone, idle_timeout, 30000),
State = #state{idle_timeout = IdleTimout, State = #state{idle_timeout = IdleTimout,
clean_start = CleanStart, clean_start = CleanStart,
binding = binding(ConnPid), deliver_fun = deliver_fun(ConnPid),
client_id = ClientId, client_id = ClientId,
username = Username, username = Username,
conn_pid = ConnPid, conn_pid = ConnPid,
@ -376,9 +381,18 @@ init_mqueue(Zone) ->
default_priority => get_env(Zone, mqueue_default_priority) default_priority => get_env(Zone, mqueue_default_priority)
}). }).
binding(undefined) -> undefined;
binding(ConnPid) -> binding(ConnPid) ->
case node(ConnPid) =:= node() of true -> local; false -> remote end. case node(ConnPid) =:= node() of true -> local; false -> remote end.
deliver_fun(ConnPid) when node(ConnPid) == node() ->
fun(Packet) -> ConnPid ! {deliver, Packet}, ok end;
deliver_fun(ConnPid) ->
Node = node(ConnPid),
fun(Packet) ->
emqx_rpc:cast(Node, erlang, send, [ConnPid, {deliver, Packet}])
end.
handle_call(info, _From, State) -> handle_call(info, _From, State) ->
reply(info(State), State); reply(info(State), State);
@ -539,7 +553,7 @@ handle_cast({resume, #{conn_pid := ConnPid,
true = link(ConnPid), true = link(ConnPid),
State1 = State#state{conn_pid = ConnPid, State1 = State#state{conn_pid = ConnPid,
binding = binding(ConnPid), deliver_fun = deliver_fun(ConnPid),
old_conn_pid = OldConnPid, old_conn_pid = OldConnPid,
clean_start = false, clean_start = false,
retry_timer = undefined, retry_timer = undefined,
@ -566,25 +580,11 @@ handle_cast(Msg, State) ->
emqx_logger:error("[Session] unexpected cast: ~p", [Msg]), emqx_logger:error("[Session] unexpected cast: ~p", [Msg]),
{noreply, State}. {noreply, State}.
%% Batch dispatch handle_info({dispatch, Topic, Msg}, State) when is_record(Msg, message) ->
handle_info({dispatch, Topic, Msgs}, State) when is_list(Msgs) -> handle_dispatch([{Topic, Msg}], State);
noreply(lists:foldl(
fun(Msg, St) ->
element(2, handle_info({dispatch, Topic, Msg}, St))
end, State, Msgs));
%% Dispatch message handle_info({dispatch, Topic, Msgs}, State) when is_list(Msgs) ->
handle_info({dispatch, Topic, Msg = #message{}}, State) -> handle_dispatch([{Topic, Msg} || Msg <- Msgs], State);
case emqx_shared_sub:is_ack_required(Msg) andalso not has_connection(State) of
true ->
%% Require ack, but we do not have connection
%% negative ack the message so it can try the next subscriber in the group
ok = emqx_shared_sub:nack_no_connection(Msg),
{noreply, State};
false ->
NewState = handle_dispatch(Topic, Msg, State),
noreply(ensure_stats_timer(maybe_gc({1, msg_size(Msg)}, NewState)))
end;
%% Do nothing if the client has been disconnected. %% Do nothing if the client has been disconnected.
handle_info({timeout, Timer, retry_delivery}, State = #state{conn_pid = undefined, retry_timer = Timer}) -> handle_info({timeout, Timer, retry_delivery}, State = #state{conn_pid = undefined, retry_timer = Timer}) ->
@ -684,18 +684,11 @@ maybe_shutdown(Pid, Reason) ->
%% Internal functions %% Internal functions
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
has_connection(#state{conn_pid = Pid}) -> is_connection_alive(#state{conn_pid = Pid}) ->
is_pid(Pid) andalso is_process_alive(Pid). is_pid(Pid) andalso is_process_alive(Pid).
handle_dispatch(Topic, Msg, State = #state{subscriptions = SubMap}) -> %%------------------------------------------------------------------------------
case maps:find(Topic, SubMap) of %% Suback and unsuback
{ok, #{nl := Nl, qos := QoS, rap := Rap, subid := SubId}} ->
run_dispatch_steps([{nl, Nl}, {qos, QoS}, {rap, Rap}, {subid, SubId}], Msg, State);
{ok, #{nl := Nl, qos := QoS, rap := Rap}} ->
run_dispatch_steps([{nl, Nl}, {qos, QoS}, {rap, Rap}], Msg, State);
error ->
dispatch(emqx_message:unset_flag(dup, Msg), State)
end.
suback(_From, undefined, _ReasonCodes) -> suback(_From, undefined, _ReasonCodes) ->
ignore; ignore;
@ -722,7 +715,6 @@ kick(ClientId, OldConnPid, ConnPid) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Replay or Retry Delivery %% Replay or Retry Delivery
%%------------------------------------------------------------------------------
%% Redeliver at once if force is true %% Redeliver at once if force is true
retry_delivery(Force, State = #state{inflight = Inflight}) -> retry_delivery(Force, State = #state{inflight = Inflight}) ->
@ -766,6 +758,7 @@ retry_delivery(Force, [{Type, Msg0, Ts} | Msgs], Now,
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Send Will Message %% Send Will Message
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
send_willmsg(undefined) -> send_willmsg(undefined) ->
ignore; ignore;
send_willmsg(WillMsg) -> send_willmsg(WillMsg) ->
@ -801,80 +794,73 @@ expire_awaiting_rel([{PacketId, Ts} | More], Now,
is_awaiting_full(#state{max_awaiting_rel = 0}) -> is_awaiting_full(#state{max_awaiting_rel = 0}) ->
false; false;
is_awaiting_full(#state{awaiting_rel = AwaitingRel, max_awaiting_rel = MaxLen}) -> is_awaiting_full(#state{awaiting_rel = AwaitingRel,
max_awaiting_rel = MaxLen}) ->
maps:size(AwaitingRel) >= MaxLen. maps:size(AwaitingRel) >= MaxLen.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Dispatch Messages %% Dispatch messages
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
run_dispatch_steps([], Msg, State) -> handle_dispatch(Msgs, State = #state{inflight = Inflight, subscriptions = SubMap}) ->
dispatch(Msg, State); %% Drain the mailbox and batch deliver
run_dispatch_steps([{nl, 1}|_Steps], #message{from = ClientId}, State = #state{client_id = ClientId}) -> Msgs1 = drain_m(batch_n(Inflight), Msgs),
State; %% Ack the messages for shared subscription
run_dispatch_steps([{nl, _}|Steps], Msg, State) -> Msgs2 = maybe_ack_shared(Msgs1, State),
run_dispatch_steps(Steps, Msg, State); %% Process suboptions
run_dispatch_steps([{qos, SubQoS}|Steps], Msg0 = #message{qos = PubQoS}, State = #state{upgrade_qos = false}) -> Msgs3 = lists:foldr(
%% Ack immediately if a shared dispatch QoS is downgraded to 0 fun({Topic, Msg}, Acc) ->
Msg = case SubQoS =:= ?QOS_0 of SubOpts = find_subopts(Topic, SubMap),
true -> emqx_shared_sub:maybe_ack(Msg0); case process_subopts(SubOpts, Msg, State) of
false -> Msg0 {ok, Msg1} -> [Msg1|Acc];
end, ignore -> Acc
run_dispatch_steps(Steps, Msg#message{qos = min(SubQoS, PubQoS)}, State); end
run_dispatch_steps([{qos, SubQoS}|Steps], Msg = #message{qos = PubQoS}, State = #state{upgrade_qos = true}) -> end, [], Msgs2),
run_dispatch_steps(Steps, Msg#message{qos = max(SubQoS, PubQoS)}, State); NState = batch_process(Msgs3, State),
run_dispatch_steps([{rap, _Rap}|Steps], Msg = #message{flags = Flags, headers = #{retained := true}}, State = #state{}) -> noreply(ensure_stats_timer(NState)).
run_dispatch_steps(Steps, Msg#message{flags = maps:put(retain, true, Flags)}, State);
run_dispatch_steps([{rap, 0}|Steps], Msg = #message{flags = Flags}, State = #state{}) ->
run_dispatch_steps(Steps, Msg#message{flags = maps:put(retain, false, Flags)}, State);
run_dispatch_steps([{rap, _}|Steps], Msg, State) ->
run_dispatch_steps(Steps, Msg, State);
run_dispatch_steps([{subid, SubId}|Steps], Msg, State) ->
run_dispatch_steps(Steps, emqx_message:set_header('Subscription-Identifier', SubId, Msg), State).
%% Enqueue message if the client has been disconnected batch_n(Inflight) ->
dispatch(Msg, State = #state{client_id = ClientId, username = Username, conn_pid = undefined}) -> case emqx_inflight:max_size(Inflight) of
case emqx_hooks:run('message.dropped', [#{client_id => ClientId, username => Username}, Msg]) of 0 -> ?DEFAULT_BATCH_N;
ok -> enqueue_msg(Msg, State); Sz -> Sz - emqx_inflight:size(Inflight)
stop -> State
end;
%% Deliver qos0 message directly to client
dispatch(Msg = #message{qos = ?QOS_0} = Msg, State) ->
ok = deliver(undefined, Msg, State),
State;
dispatch(Msg = #message{qos = QoS} = Msg,
State = #state{next_pkt_id = PacketId, inflight = Inflight})
when QoS =:= ?QOS_1 orelse QoS =:= ?QOS_2 ->
case emqx_inflight:is_full(Inflight) of
true ->
enqueue_msg(Msg, State);
false ->
ok = deliver(PacketId, Msg, State),
await(PacketId, Msg, next_pkt_id(State))
end. end.
enqueue_msg(Msg, State = #state{mqueue = Q}) -> drain_m(Cnt, Msgs) when Cnt =< 0 ->
emqx_pd:update_counter(enqueue_stats, 1), lists:reverse(Msgs);
{Dropped, NewQ} = emqx_mqueue:in(Msg, Q), drain_m(Cnt, Msgs) ->
Dropped =/= undefined andalso emqx_shared_sub:maybe_nack_dropped(Dropped), receive
State#state{mqueue = NewQ}. {dispatch, Topic, Msg} ->
drain_m(Cnt-1, [{Topic, Msg}|Msgs])
after 0 ->
lists:reverse(Msgs)
end.
%%------------------------------------------------------------------------------ %% Ack or nack the messages of shared subscription?
%% Deliver maybe_ack_shared(Msgs, State) when is_list(Msgs) ->
%%------------------------------------------------------------------------------ lists:foldr(
fun({Topic, Msg}, Acc) ->
case maybe_ack_shared(Msg, State) of
ok -> Acc;
Msg1 -> [{Topic, Msg1}|Acc]
end
end, [], Msgs);
redeliver({PacketId, Msg = #message{qos = QoS}}, State) -> maybe_ack_shared(Msg, State) ->
deliver(PacketId, if QoS =:= ?QOS_2 -> Msg; case emqx_shared_sub:is_ack_required(Msg) of
true -> emqx_message:set_flag(dup, Msg) true -> do_ack_shared(Msg, State);
end, State); false -> Msg
end.
redeliver({pubrel, PacketId}, #state{conn_pid = ConnPid}) -> do_ack_shared(Msg, State = #state{inflight = Inflight}) ->
ConnPid ! {deliver, {pubrel, PacketId}}. case {is_connection_alive(State),
emqx_inflight:is_full(Inflight)} of
deliver(PacketId, Msg, State) -> {false, _} ->
emqx_pd:update_counter(deliver_stats, 1), %% Require ack, but we do not have connection
%% negative ack the message so it can try the next subscriber in the group
emqx_shared_sub:nack_no_connection(Msg);
{_, true} ->
emqx_shared_sub:maybe_nack_dropped(Msg);
_ ->
%% Ack QoS1/QoS2 messages when message is delivered to connection. %% Ack QoS1/QoS2 messages when message is delivered to connection.
%% NOTE: NOT to wait for PUBACK because: %% NOTE: NOT to wait for PUBACK because:
%% The sender is monitoring this session process, %% The sender is monitoring this session process,
@ -882,12 +868,105 @@ deliver(PacketId, Msg, State) ->
%% sender will try to dispatch the message to the next shared subscriber. %% sender will try to dispatch the message to the next shared subscriber.
%% This violates spec as QoS2 messages are not allowed to be sent to more %% This violates spec as QoS2 messages are not allowed to be sent to more
%% than one member in the group. %% than one member in the group.
do_deliver(PacketId, emqx_shared_sub:maybe_ack(Msg), State). emqx_shared_sub:maybe_ack(Msg)
end.
do_deliver(PacketId, Msg, #state{conn_pid = ConnPid, binding = local}) -> process_subopts([], Msg, _State) ->
ConnPid ! {deliver, {publish, PacketId, Msg}}, ok; {ok, Msg};
do_deliver(PacketId, Msg, #state{conn_pid = ConnPid, binding = remote}) -> process_subopts([{nl, 1}|_Opts], #message{from = ClientId}, #state{client_id = ClientId}) ->
emqx_rpc:cast(node(ConnPid), erlang, send, [ConnPid, {deliver, {publish, PacketId, Msg}}]). ignore;
process_subopts([{nl, _}|Opts], Msg, State) ->
process_subopts(Opts, Msg, State);
process_subopts([{qos, SubQoS}|Opts], Msg = #message{qos = PubQoS}, State = #state{upgrade_qos = false}) ->
process_subopts(Opts, Msg#message{qos = min(SubQoS, PubQoS)}, State);
process_subopts([{qos, SubQoS}|Opts], Msg = #message{qos = PubQoS}, State = #state{upgrade_qos = true}) ->
process_subopts(Opts, Msg#message{qos = max(SubQoS, PubQoS)}, State);
process_subopts([{rap, _Rap}|Opts], Msg = #message{flags = Flags, headers = #{retained := true}}, State = #state{}) ->
process_subopts(Opts, Msg#message{flags = maps:put(retain, true, Flags)}, State);
process_subopts([{rap, 0}|Opts], Msg = #message{flags = Flags}, State = #state{}) ->
process_subopts(Opts, Msg#message{flags = maps:put(retain, false, Flags)}, State);
process_subopts([{rap, _}|Opts], Msg, State) ->
process_subopts(Opts, Msg, State);
process_subopts([{subid, SubId}|Opts], Msg, State) ->
process_subopts(Opts, emqx_message:set_header('Subscription-Identifier', SubId, Msg), State).
find_subopts(Topic, SubMap) ->
case maps:find(Topic, SubMap) of
{ok, #{nl := Nl, qos := QoS, rap := Rap, subid := SubId}} ->
[{nl, Nl}, {qos, QoS}, {rap, Rap}, {subid, SubId}];
{ok, #{nl := Nl, qos := QoS, rap := Rap}} ->
[{nl, Nl}, {qos, QoS}, {rap, Rap}];
error -> []
end.
batch_process(Msgs, State) ->
{ok, Publishes, NState} = process_msgs(Msgs, [], State),
ok = batch_deliver(Publishes, NState),
maybe_gc(msg_cnt(Msgs), NState).
process_msgs([], Publishes, State) ->
{ok, lists:reverse(Publishes), State};
process_msgs([Msg|Msgs], Publishes, State) ->
case process_msg(Msg, State) of
{ok, Publish, NState} ->
process_msgs(Msgs, [Publish|Publishes], NState);
{ignore, NState} ->
process_msgs(Msgs, Publishes, NState)
end.
%% Enqueue message if the client has been disconnected
process_msg(Msg, State = #state{conn_pid = undefined}) ->
{ignore, enqueue_msg(Msg, State)};
%% Prepare the qos0 message delivery
process_msg(Msg = #message{qos = ?QOS_0}, State) ->
{ok, {publish, undefined, Msg}, State};
process_msg(Msg = #message{qos = QoS},
State = #state{next_pkt_id = PacketId, inflight = Inflight})
when QoS =:= ?QOS_1 orelse QoS =:= ?QOS_2 ->
case emqx_inflight:is_full(Inflight) of
true ->
{ignore, enqueue_msg(Msg, State)};
false ->
Publish = {publish, PacketId, Msg},
NState = await(PacketId, Msg, State),
{ok, Publish, next_pkt_id(NState)}
end.
enqueue_msg(Msg, State = #state{mqueue = Q, client_id = ClientId, username = Username}) ->
emqx_pd:update_counter(enqueue_stats, 1),
{Dropped, NewQ} = emqx_mqueue:in(Msg, Q),
if
Dropped =/= undefined ->
SessProps = #{client_id => ClientId, username => Username},
emqx_hooks:run('message.dropped', [SessProps, Msg]);
true -> ok
end,
State#state{mqueue = NewQ}.
%%------------------------------------------------------------------------------
%% Deliver
%%------------------------------------------------------------------------------
redeliver({PacketId, Msg = #message{qos = QoS}}, State) ->
Msg1 = if
QoS =:= ?QOS_2 -> Msg;
true -> emqx_message:set_flag(dup, Msg)
end,
do_deliver(PacketId, Msg1, State);
redeliver({pubrel, PacketId}, #state{deliver_fun = DeliverFun}) ->
DeliverFun({pubrel, PacketId}).
do_deliver(PacketId, Msg, #state{deliver_fun = DeliverFun}) ->
emqx_pd:update_counter(deliver_stats, 1),
DeliverFun({publish, PacketId, Msg}).
batch_deliver(Publishes, #state{deliver_fun = DeliverFun}) ->
emqx_pd:update_counter(deliver_stats, length(Publishes)),
DeliverFun(Publishes).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Awaiting ACK for QoS1/QoS2 Messages %% Awaiting ACK for QoS1/QoS2 Messages
@ -932,26 +1011,31 @@ acked(pubcomp, PacketId, State = #state{inflight = Inflight}) ->
dequeue(State = #state{conn_pid = undefined}) -> dequeue(State = #state{conn_pid = undefined}) ->
State; State;
dequeue(State = #state{inflight = Inflight}) -> dequeue(State = #state{inflight = Inflight, mqueue = Q}) ->
case emqx_inflight:is_full(Inflight) of case emqx_mqueue:is_empty(Q)
orelse emqx_inflight:is_full(Inflight) of
true -> State; true -> State;
false -> dequeue2(State) false ->
{Msgs, Q1} = drain_q(batch_n(Inflight), [], Q),
batch_process(lists:reverse(Msgs), State#state{mqueue = Q1})
end. end.
dequeue2(State = #state{mqueue = Q}) -> drain_q(Cnt, Msgs, Q) when Cnt =< 0 ->
{Msgs, Q};
drain_q(Cnt, Msgs, Q) ->
case emqx_mqueue:out(Q) of case emqx_mqueue:out(Q) of
{empty, _Q} -> State; {empty, _Q} -> {Msgs, Q};
{{value, Msg}, Q1} -> {{value, Msg}, Q1} ->
%% Dequeue more drain_q(Cnt-1, [Msg|Msgs], Q1)
dequeue(dispatch(Msg, State#state{mqueue = Q1}))
end. end.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Ensure timers %% Ensure timers
ensure_await_rel_timer(State = #state{await_rel_timer = undefined, await_rel_timeout = Timeout}) -> ensure_await_rel_timer(State = #state{await_rel_timer = undefined,
await_rel_timeout = Timeout}) ->
ensure_await_rel_timer(Timeout, State); ensure_await_rel_timer(Timeout, State);
ensure_await_rel_timer(State) -> ensure_await_rel_timer(State) ->
State. State.
@ -960,7 +1044,8 @@ ensure_await_rel_timer(Timeout, State = #state{await_rel_timer = undefined}) ->
ensure_await_rel_timer(_Timeout, State) -> ensure_await_rel_timer(_Timeout, State) ->
State. State.
ensure_retry_timer(State = #state{retry_timer = undefined, retry_interval = Interval}) -> ensure_retry_timer(State = #state{retry_timer = undefined,
retry_interval = Interval}) ->
ensure_retry_timer(Interval, State); ensure_retry_timer(Interval, State);
ensure_retry_timer(State) -> ensure_retry_timer(State) ->
State. State.
@ -970,7 +1055,8 @@ ensure_retry_timer(Interval, State = #state{retry_timer = undefined}) ->
ensure_retry_timer(_Timeout, State) -> ensure_retry_timer(_Timeout, State) ->
State. State.
ensure_expire_timer(State = #state{expiry_interval = Interval}) when Interval > 0 andalso Interval =/= 16#ffffffff -> ensure_expire_timer(State = #state{expiry_interval = Interval})
when Interval > 0 andalso Interval =/= 16#ffffffff ->
State#state{expiry_timer = emqx_misc:start_timer(Interval * 1000, expired)}; State#state{expiry_timer = emqx_misc:start_timer(Interval * 1000, expired)};
ensure_expire_timer(State) -> ensure_expire_timer(State) ->
State. State.
@ -997,15 +1083,20 @@ next_pkt_id(State = #state{next_pkt_id = 16#FFFF}) ->
next_pkt_id(State = #state{next_pkt_id = Id}) -> next_pkt_id(State = #state{next_pkt_id = Id}) ->
State#state{next_pkt_id = Id + 1}. State#state{next_pkt_id = Id + 1}.
%%------------------------------------------------------------------------------
%% Maybe GC
msg_cnt(Msgs) ->
lists:foldl(fun(Msg, {Cnt, Oct}) ->
{Cnt+1, Oct+msg_size(Msg)}
end, {0, 0}, Msgs).
%% Take only the payload size into account, add other fields if necessary %% Take only the payload size into account, add other fields if necessary
msg_size(#message{payload = Payload}) -> payload_size(Payload). msg_size(#message{payload = Payload}) -> payload_size(Payload).
%% Payload should be binary(), but not 100% sure. Need dialyzer! %% Payload should be binary(), but not 100% sure. Need dialyzer!
payload_size(Payload) -> erlang:iolist_size(Payload). payload_size(Payload) -> erlang:iolist_size(Payload).
%%------------------------------------------------------------------------------
%% Maybe GC
maybe_gc(_, State = #state{gc_state = undefined}) -> maybe_gc(_, State = #state{gc_state = undefined}) ->
State; State;
maybe_gc({Cnt, Oct}, State = #state{gc_state = GCSt}) -> maybe_gc({Cnt, Oct}, State = #state{gc_state = GCSt}) ->

View File

@ -18,7 +18,8 @@
-include("emqx_mqtt.hrl"). -include("emqx_mqtt.hrl").
-include("logger.hrl"). -include("logger.hrl").
-export([info/1, attrs/1]). -export([info/1]).
-export([attrs/1]).
-export([stats/1]). -export([stats/1]).
-export([kick/1]). -export([kick/1]).
-export([session/1]). -export([session/1]).
@ -37,7 +38,7 @@
sockname, sockname,
idle_timeout, idle_timeout,
proto_state, proto_state,
parser_state, parse_state,
keepalive, keepalive,
enable_stats, enable_stats,
stats_timer, stats_timer,
@ -128,24 +129,21 @@ websocket_init(#state{request = Req, options = Options}) ->
sockname => Sockname, sockname => Sockname,
peercert => Peercert, peercert => Peercert,
sendfun => send_fun(self())}, Options), sendfun => send_fun(self())}, Options),
ParserState = emqx_protocol:parser(ProtoState), ParseState = emqx_protocol:parser(ProtoState),
Zone = proplists:get_value(zone, Options), Zone = proplists:get_value(zone, Options),
EnableStats = emqx_zone:get_env(Zone, enable_stats, true), EnableStats = emqx_zone:get_env(Zone, enable_stats, true),
IdleTimout = emqx_zone:get_env(Zone, idle_timeout, 30000), IdleTimout = emqx_zone:get_env(Zone, idle_timeout, 30000),
emqx_logger:set_metadata_peername(esockd_net:format(Peername)), emqx_logger:set_metadata_peername(esockd_net:format(Peername)),
{ok, #state{peername = Peername, {ok, #state{peername = Peername,
sockname = Sockname, sockname = Sockname,
parser_state = ParserState, parse_state = ParseState,
proto_state = ProtoState, proto_state = ProtoState,
enable_stats = EnableStats, enable_stats = EnableStats,
idle_timeout = IdleTimout}}. idle_timeout = IdleTimout}}.
send_fun(WsPid) -> send_fun(WsPid) ->
fun(Packet, Options) -> fun(Data) ->
Data = emqx_frame:serialize(Packet, Options),
BinSize = iolist_size(Data), BinSize = iolist_size(Data),
emqx_metrics:trans(inc, 'bytes/sent', BinSize),
emqx_pd:update_counter(send_cnt, 1), emqx_pd:update_counter(send_cnt, 1),
emqx_pd:update_counter(send_oct, BinSize), emqx_pd:update_counter(send_oct, BinSize),
WsPid ! {binary, iolist_to_binary(Data)}, WsPid ! {binary, iolist_to_binary(Data)},
@ -159,15 +157,15 @@ websocket_handle({binary, <<>>}, State) ->
{ok, ensure_stats_timer(State)}; {ok, ensure_stats_timer(State)};
websocket_handle({binary, [<<>>]}, State) -> websocket_handle({binary, [<<>>]}, State) ->
{ok, ensure_stats_timer(State)}; {ok, ensure_stats_timer(State)};
websocket_handle({binary, Data}, State = #state{parser_state = ParserState, websocket_handle({binary, Data}, State = #state{parse_state = ParseState,
proto_state = ProtoState}) -> proto_state = ProtoState}) ->
?LOG(debug, "RECV ~p", [Data]), ?LOG(debug, "RECV ~p", [Data]),
BinSize = iolist_size(Data), BinSize = iolist_size(Data),
emqx_pd:update_counter(recv_oct, BinSize), emqx_pd:update_counter(recv_oct, BinSize),
emqx_metrics:trans(inc, 'bytes/received', BinSize), emqx_metrics:trans(inc, 'bytes/received', BinSize),
try emqx_frame:parse(iolist_to_binary(Data), ParserState) of try emqx_frame:parse(iolist_to_binary(Data), ParseState) of
{more, ParserState1} -> {more, ParseState1} ->
{ok, State#state{parser_state = ParserState1}}; {ok, State#state{parse_state = ParseState1}};
{ok, Packet, Rest} -> {ok, Packet, Rest} ->
emqx_metrics:received(Packet), emqx_metrics:received(Packet),
emqx_pd:update_counter(recv_cnt, 1), emqx_pd:update_counter(recv_cnt, 1),
@ -248,10 +246,10 @@ websocket_info({keepalive, check}, State = #state{keepalive = KeepAlive}) ->
{ok, KeepAlive1} -> {ok, KeepAlive1} ->
{ok, State#state{keepalive = KeepAlive1}}; {ok, State#state{keepalive = KeepAlive1}};
{error, timeout} -> {error, timeout} ->
?LOG(debug, "Keepalive Timeout!", []), ?LOG(debug, "Keepalive Timeout!"),
shutdown(keepalive_timeout, State); shutdown(keepalive_timeout, State);
{error, Error} -> {error, Error} ->
?LOG(warning, "Keepalive error - ~p", [Error]), ?LOG(error, "Keepalive error - ~p", [Error]),
shutdown(keepalive_error, State) shutdown(keepalive_error, State)
end; end;
@ -277,15 +275,14 @@ terminate(SockError, _Req, #state{keepalive = Keepalive,
proto_state = ProtoState, proto_state = ProtoState,
shutdown = Shutdown}) -> shutdown = Shutdown}) ->
?LOG(debug, "Terminated for ~p, sockerror: ~p", ?LOG(debug, "Terminated for ~p, sockerror: ~p", [Shutdown, SockError]),
[Shutdown, SockError]),
emqx_keepalive:cancel(Keepalive), emqx_keepalive:cancel(Keepalive),
case {ProtoState, Shutdown} of case {ProtoState, Shutdown} of
{undefined, _} -> ok; {undefined, _} -> ok;
{_, {shutdown, Reason}} -> {_, {shutdown, Reason}} ->
emqx_protocol:shutdown(Reason, ProtoState); emqx_protocol:terminate(Reason, ProtoState);
{_, Error} -> {_, Error} ->
emqx_protocol:shutdown(Error, ProtoState) emqx_protocol:terminate(Error, ProtoState)
end. end.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -293,7 +290,7 @@ terminate(SockError, _Req, #state{keepalive = Keepalive,
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
reset_parser(State = #state{proto_state = ProtoState}) -> reset_parser(State = #state{proto_state = ProtoState}) ->
State#state{parser_state = emqx_protocol:parser(ProtoState)}. State#state{parse_state = emqx_protocol:parser(ProtoState)}.
ensure_stats_timer(State = #state{enable_stats = true, ensure_stats_timer(State = #state{enable_stats = true,
stats_timer = undefined, stats_timer = undefined,

View File

@ -96,6 +96,7 @@ t_connect_api(_Config) ->
?STATS = emqx_connection:stats(CPid), ?STATS = emqx_connection:stats(CPid),
?ATTRS = emqx_connection:attrs(CPid), ?ATTRS = emqx_connection:attrs(CPid),
?INFO = emqx_connection:info(CPid), ?INFO = emqx_connection:info(CPid),
SessionPid = emqx_connection:session(CPid), SPid = emqx_connection:session(CPid),
true = is_pid(SessionPid), true = is_pid(SPid),
emqx_client:disconnect(T1). emqx_client:disconnect(T1).

View File

@ -1,18 +1,15 @@
%%%=================================================================== %% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
%%% Copyright (c) 2013-2019 EMQ Inc. All rights reserved. %%
%%% %% Licensed under the Apache License, Version 2.0 (the "License");
%%% Licensed under the Apache License, Version 2.0 (the "License"); %% you may not use this file except in compliance with the License.
%%% you may not use this file except in compliance with the License. %% You may obtain a copy of the License at
%%% You may obtain a copy of the License at %%
%%% %% http://www.apache.org/licenses/LICENSE-2.0
%%% http://www.apache.org/licenses/LICENSE-2.0 %%
%%% %% Unless required by applicable law or agreed to in writing, software
%%% Unless required by applicable law or agreed to in writing, software %% distributed under the License is distributed on an "AS IS" BASIS,
%%% distributed under the License is distributed on an "AS IS" BASIS, %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %% See the License for the specific language governing permissions and
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%===================================================================
-module(emqx_mqtt_packet_SUITE). -module(emqx_mqtt_packet_SUITE).

View File

@ -62,7 +62,7 @@ async_submit_mfa(_Config) ->
emqx_pool:async_submit(fun ?MODULE:test_mfa/0, []). emqx_pool:async_submit(fun ?MODULE:test_mfa/0, []).
async_submit_crash(_) -> async_submit_crash(_) ->
emqx_pool:async_submit(fun() -> error(test) end). emqx_pool:async_submit(fun() -> error(unexpected_error) end).
t_unexpected(_) -> t_unexpected(_) ->
Pid = emqx_pool:worker(), Pid = emqx_pool:worker(),
@ -73,3 +73,4 @@ t_unexpected(_) ->
test_mfa() -> test_mfa() ->
lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]). lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]).

View File

@ -1,5 +1,4 @@
%%-------------------------------------------------------------------- %% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
%% Copyright (c) 2013-2019 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -12,7 +11,6 @@
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and %% See the License for the specific language governing permissions and
%% limitations under the License. %% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_protocol_SUITE). -module(emqx_protocol_SUITE).
@ -574,9 +572,9 @@ acl_deny_action_ct(_) ->
acl_deny_action_eunit(_) -> acl_deny_action_eunit(_) ->
PState = ?TEST_PSTATE(?MQTT_PROTO_V5, #{msg => 0, pkt => 0}), PState = ?TEST_PSTATE(?MQTT_PROTO_V5, #{msg => 0, pkt => 0}),
CodeName = emqx_reason_codes:name(?RC_NOT_AUTHORIZED, ?MQTT_PROTO_V5), CodeName = emqx_reason_codes:name(?RC_NOT_AUTHORIZED, ?MQTT_PROTO_V5),
{error, CodeName, NEWPSTATE1} = emqx_protocol:process_packet(?PUBLISH_PACKET(?QOS_1, <<"acl_deny_action">>, 1, <<"payload">>), PState), {error, CodeName, NEWPSTATE1} = emqx_protocol:process(?PUBLISH_PACKET(?QOS_1, <<"acl_deny_action">>, 1, <<"payload">>), PState),
?assertEqual(#{pkt => 1, msg => 0}, NEWPSTATE1#pstate.send_stats), ?assertEqual(#{pkt => 1, msg => 0}, NEWPSTATE1#pstate.send_stats),
{error, CodeName, NEWPSTATE2} = emqx_protocol:process_packet(?PUBLISH_PACKET(?QOS_2, <<"acl_deny_action">>, 2, <<"payload">>), PState), {error, CodeName, NEWPSTATE2} = emqx_protocol:process(?PUBLISH_PACKET(?QOS_2, <<"acl_deny_action">>, 2, <<"payload">>), PState),
?assertEqual(#{pkt => 1, msg => 0}, NEWPSTATE2#pstate.send_stats). ?assertEqual(#{pkt => 1, msg => 0}, NEWPSTATE2#pstate.send_stats).
will_topic_check(_) -> will_topic_check(_) ->

View File

@ -1,4 +1,3 @@
%% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved. %% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
@ -45,7 +44,7 @@ ignore_loop(_Config) ->
application:set_env(emqx, mqtt_ignore_loop_deliver, false). application:set_env(emqx, mqtt_ignore_loop_deliver, false).
t_session_all(_) -> t_session_all(_) ->
emqx_zone:set_env(internal, idle_timeout, 100), emqx_zone:set_env(internal, idle_timeout, 1000),
ClientId = <<"ClientId">>, ClientId = <<"ClientId">>,
{ok, ConnPid} = emqx_mock_client:start_link(ClientId), {ok, ConnPid} = emqx_mock_client:start_link(ClientId),
{ok, SPid} = emqx_mock_client:open_session(ConnPid, ClientId, internal), {ok, SPid} = emqx_mock_client:open_session(ConnPid, ClientId, internal),
@ -56,7 +55,7 @@ t_session_all(_) ->
[{<<"topic">>, _}] = emqx:subscriptions(SPid), [{<<"topic">>, _}] = emqx:subscriptions(SPid),
emqx_session:publish(SPid, 1, Message1), emqx_session:publish(SPid, 1, Message1),
timer:sleep(200), timer:sleep(200),
{publish, 1, _} = emqx_mock_client:get_last_message(ConnPid), [{publish, 1, _}] = emqx_mock_client:get_last_message(ConnPid),
Attrs = emqx_session:attrs(SPid), Attrs = emqx_session:attrs(SPid),
Info = emqx_session:info(SPid), Info = emqx_session:info(SPid),
Stats = emqx_session:stats(SPid), Stats = emqx_session:stats(SPid),

View File

@ -59,7 +59,7 @@ t_random_basic(_) ->
PacketId = 1, PacketId = 1,
emqx_session:publish(SPid, PacketId, Message1), emqx_session:publish(SPid, PacketId, Message1),
?wait(case emqx_mock_client:get_last_message(ConnPid) of ?wait(case emqx_mock_client:get_last_message(ConnPid) of
{publish, 1, _} -> true; [{publish, 1, _}] -> true;
Other -> Other Other -> Other
end, 1000), end, 1000),
emqx_session:pubrec(SPid, PacketId, reasoncode), emqx_session:pubrec(SPid, PacketId, reasoncode),
@ -105,7 +105,7 @@ t_no_connection_nack(_) ->
fun(PacketId, ConnPid) -> fun(PacketId, ConnPid) ->
Payload = MkPayload(PacketId), Payload = MkPayload(PacketId),
case emqx_mock_client:get_last_message(ConnPid) of case emqx_mock_client:get_last_message(ConnPid) of
{publish, _, #message{payload = Payload}} -> [{publish, _, #message{payload = Payload}}] ->
CasePid ! {Ref, PacketId, ConnPid}, CasePid ! {Ref, PacketId, ConnPid},
true; true;
_Other -> _Other ->
@ -176,7 +176,7 @@ t_not_so_sticky(_) ->
?wait(subscribed(<<"group1">>, <<"foo/bar">>, SPid1), 1000), ?wait(subscribed(<<"group1">>, <<"foo/bar">>, SPid1), 1000),
emqx_session:publish(SPid1, 1, Message1), emqx_session:publish(SPid1, 1, Message1),
?wait(case emqx_mock_client:get_last_message(ConnPid1) of ?wait(case emqx_mock_client:get_last_message(ConnPid1) of
{publish, _, #message{payload = <<"hello1">>}} -> true; [{publish, _, #message{payload = <<"hello1">>}}] -> true;
Other -> Other Other -> Other
end, 1000), end, 1000),
emqx_mock_client:close_session(ConnPid1), emqx_mock_client:close_session(ConnPid1),
@ -185,7 +185,7 @@ t_not_so_sticky(_) ->
?wait(subscribed(<<"group1">>, <<"foo/#">>, SPid2), 1000), ?wait(subscribed(<<"group1">>, <<"foo/#">>, SPid2), 1000),
emqx_session:publish(SPid2, 2, Message2), emqx_session:publish(SPid2, 2, Message2),
?wait(case emqx_mock_client:get_last_message(ConnPid2) of ?wait(case emqx_mock_client:get_last_message(ConnPid2) of
{publish, _, #message{payload = <<"hello2">>}} -> true; [{publish, _, #message{payload = <<"hello2">>}}] -> true;
Other -> Other Other -> Other
end, 1000), end, 1000),
emqx_mock_client:close_session(ConnPid2), emqx_mock_client:close_session(ConnPid2),
@ -240,7 +240,7 @@ test_two_messages(Strategy, WithAck) ->
last_message(_ExpectedPayload, []) -> <<"not yet?">>; last_message(_ExpectedPayload, []) -> <<"not yet?">>;
last_message(ExpectedPayload, [Pid | Pids]) -> last_message(ExpectedPayload, [Pid | Pids]) ->
case emqx_mock_client:get_last_message(Pid) of case emqx_mock_client:get_last_message(Pid) of
{publish, _, #message{payload = ExpectedPayload}} -> {true, Pid}; [{publish, _, #message{payload = ExpectedPayload}}] -> {true, Pid};
_Other -> last_message(ExpectedPayload, Pids) _Other -> last_message(ExpectedPayload, Pids)
end. end.