Improve the emqx_connection module using gen_statem behaviour (#2235)

This commit is contained in:
Feng Lee 2019-02-28 15:08:29 +08:00 committed by Gilbert
parent c3e6f3c3b2
commit 08204fc7a8
10 changed files with 419 additions and 373 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,28 +50,29 @@
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);
info(#state{transport = Transport, info(#state{transport = Transport,
socket = Socket, socket = Socket,
peername = Peername, peername = Peername,
sockname = Sockname, sockname = Sockname,
conn_state = ConnState, conn_state = ConnState,
active_n = ActiveN, active_n = ActiveN,
rate_limit = RateLimit, rate_limit = RateLimit,
pub_limit = PubLimit, pub_limit = PubLimit,
proto_state = ProtoState}) -> proto_state = ProtoState}) ->
ConnInfo = [{socktype, Transport:type(Socket)}, ConnInfo = [{socktype, Transport:type(Socket)},
{peername, Peername}, {peername, Peername},
@ -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);
@ -100,277 +105,305 @@ attrs(#state{peername = Peername,
stats(CPid) when is_pid(CPid) -> stats(CPid) when is_pid(CPid) ->
call(CPid, stats); call(CPid, stats);
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), {ok, Ss} -> Ss;
case Transport:getstat(Socket, ?SOCK_STATS) of {error, _} -> []
{ok, Ss} -> Ss; end,
{error, _} -> [] lists:append([SockStats,
end]). 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} -> {ok, Peername} = Transport:ensure_ok_or_exit(peername, [Socket]),
Zone = proplists:get_value(zone, Options), {ok, Sockname} = Transport:ensure_ok_or_exit(sockname, [Socket]),
{ok, Peername} = Transport:ensure_ok_or_exit(peername, [Socket]), Peercert = Transport:ensure_ok_or_exit(peercert, [Socket]),
{ok, Sockname} = Transport:ensure_ok_or_exit(sockname, [Socket]), emqx_logger:set_metadata_peername(esockd_net:format(Peername)),
Peercert = Transport:ensure_ok_or_exit(peercert, [Socket]), 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,
active_n = ActiveN, active_n = ActiveN,
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), gen_statem:enter_loop(?MODULE, [{hibernate_after, 2 * IdleTimout}],
emqx_logger:set_metadata_peername(esockd_net:format(Peername)), idle, State, self(), [IdleTimout]).
gen_server:enter_loop(?MODULE, [{hibernate_after, 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};
Error -> Error Error -> Error
end end
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
socket = Socket, handle({call, From}, info, State) ->
keepalive = KeepAlive, reply(From, info(State), State);
proto_state = ProtoState}) ->
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,
keepalive = KeepAlive,
proto_state = ProtoState}) ->
?LOG(debug, "Terminated for ~p", [Reason]), ?LOG(debug, "Terminated for ~p", [Reason]),
Transport:fast_close(Socket), Transport:fast_close(Socket),
emqx_keepalive:cancel(KeepAlive), emqx_keepalive:cancel(KeepAlive),
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} ->
?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} -> {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

@ -56,7 +56,7 @@ start_listener(Proto, ListenOn, Options) when Proto == http; Proto == ws ->
%% Start MQTT/WSS listener %% Start MQTT/WSS listener
start_listener(Proto, ListenOn, Options) when Proto == https; Proto == wss -> start_listener(Proto, ListenOn, Options) when Proto == https; Proto == wss ->
Dispatch = cowboy_router:compile([{'_', [{mqtt_path(Options), emqx_ws_connection, Options}]}]), Dispatch = cowboy_router:compile([{'_', [{mqtt_path(Options), emqx_ws_connection, Options}]}]),
start_http_listener(fun cowboy:start_tls/3, 'mqtt:wss', ListenOn, ranch_opts(Options), Dispatch). start_http_listener(fun cowboy:start_tls/3, 'mqtt:wss', ListenOn, ranch_opts(Options), Dispatch).
start_mqtt_listener(Name, ListenOn, Options) -> start_mqtt_listener(Name, ListenOn, Options) ->

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{proto_ver = ProtoVer};
PState) ->
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,8 +312,8 @@ 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),
{error, ?RC_TOPIC_ALIAS_INVALID} {error, ?RC_TOPIC_ALIAS_INVALID}
@ -323,28 +329,28 @@ 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,
clean_start = CleanStart, clean_start = CleanStart,
keepalive = Keepalive, keepalive = Keepalive,
properties = ConnProps, properties = ConnProps,
client_id = ClientId, client_id = ClientId,
username = Username, username = Username,
password = Password} = ConnPkt), PState) -> password = Password} = ConnPkt), PState) ->
NewClientId = maybe_use_username_as_clientid(ClientId, Username, PState), NewClientId = maybe_use_username_as_clientid(ClientId, Username, PState),
@ -394,17 +400,17 @@ 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);
{error, ReasonCode} -> {error, ReasonCode} ->
?LOG(warning, "Cannot publish qos0 message to ~s for ~s", ?LOG(warning, "Cannot publish qos0 message to ~s for ~s",
[Topic, emqx_reason_codes:text(ReasonCode)]), [Topic, emqx_reason_codes:text(ReasonCode)]),
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,30 +420,28 @@ 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);
{error, ReasonCode} -> {error, ReasonCode} ->
?LOG(warning, "Cannot publish qos2 message to ~s for ~s", ?LOG(warning, "Cannot publish qos2 message to ~s for ~s",
[Topic, emqx_reason_codes:text(ReasonCode)]), [Topic, emqx_reason_codes:text(ReasonCode)]),
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,22 +457,22 @@ 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}) ->
RawTopicFilters1 = if ProtoVer < ?MQTT_PROTO_V5 -> RawTopicFilters1 = if ProtoVer < ?MQTT_PROTO_V5 ->
IfIgnoreLoop = case IgnoreLoop of true -> 1; false -> 0 end, IfIgnoreLoop = case IgnoreLoop of true -> 1; false -> 0 end,
case IsBridge of case IsBridge of
true -> [{RawTopic, SubOpts#{rap => 1, nl => IfIgnoreLoop}} || {RawTopic, SubOpts} <- RawTopicFilters]; true -> [{RawTopic, SubOpts#{rap => 1, nl => IfIgnoreLoop}} || {RawTopic, SubOpts} <- RawTopicFilters];
false -> [{RawTopic, SubOpts#{rap => 0, nl => IfIgnoreLoop}} || {RawTopic, SubOpts} <- RawTopicFilters] false -> [{RawTopic, SubOpts#{rap => 0, nl => IfIgnoreLoop}} || {RawTopic, SubOpts} <- RawTopicFilters]
end; end;
true -> true ->
RawTopicFilters RawTopicFilters
end, end,
case check_subscribe( case check_subscribe(
parse_topic_filters(?SUBSCRIBE, RawTopicFilters1), PState) of parse_topic_filters(?SUBSCRIBE, RawTopicFilters1), PState) of
{ok, TopicFilters} -> {ok, TopicFilters} ->
@ -483,15 +487,14 @@ 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
{ok, PState1} -> {ok, PState1} ->
do_acl_deny_action(Packet, ReasonCodes, PState1); do_acl_deny_action(Packet, ReasonCodes, PState1);
@ -500,8 +503,8 @@ 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
{ok, TopicFilters} -> {ok, TopicFilters} ->
@ -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).
@ -666,11 +673,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 +818,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.
@ -825,7 +833,7 @@ check_publish(Packet, PState) ->
fun check_pub_acl/2], Packet, PState). fun check_pub_acl/2], Packet, PState).
check_pub_caps(#mqtt_packet{header = #mqtt_packet_header{qos = QoS, retain = Retain}, check_pub_caps(#mqtt_packet{header = #mqtt_packet_header{qos = QoS, retain = Retain},
variable = #mqtt_packet_publish{ properties = _Properties}}, variable = #mqtt_packet_publish{properties = _Properties}},
#pstate{zone = Zone}) -> #pstate{zone = Zone}) ->
emqx_mqtt_caps:check_pub(Zone, #{qos => QoS, retain => Retain}). emqx_mqtt_caps:check_pub(Zone, #{qos => QoS, retain => Retain}).
@ -892,15 +900,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

@ -249,19 +249,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.
@ -951,7 +951,6 @@ dequeue2(State = #state{mqueue = Q}) ->
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.

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,16 @@
%%%=================================================================== %% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
%%% Copyright (c) 2013-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.
%%% 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() -> A = 1, A = 0 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-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(_) ->