Merge branch 'dev' of github.com:emqtt/emqtt into dev
This commit is contained in:
commit
c9b5c87b29
|
@ -176,12 +176,12 @@
|
||||||
%% Size of acceptor pool
|
%% Size of acceptor pool
|
||||||
{acceptors, 16},
|
{acceptors, 16},
|
||||||
%% Maximum number of concurrent clients
|
%% Maximum number of concurrent clients
|
||||||
{max_clients, 1024},
|
{max_clients, 512},
|
||||||
%% Socket Access Control
|
%% Socket Access Control
|
||||||
{access, [{allow, all}]},
|
{access, [{allow, all}]},
|
||||||
%% Socket Options
|
%% Socket Options
|
||||||
{sockopts, [
|
{sockopts, [
|
||||||
{backlog, 1024}
|
{backlog, 512}
|
||||||
%Set buffer if hight thoughtput
|
%Set buffer if hight thoughtput
|
||||||
%{recbuf, 4096},
|
%{recbuf, 4096},
|
||||||
%{sndbuf, 4096}
|
%{sndbuf, 4096}
|
||||||
|
@ -192,7 +192,7 @@
|
||||||
%% Size of acceptor pool
|
%% Size of acceptor pool
|
||||||
{acceptors, 4},
|
{acceptors, 4},
|
||||||
%% Maximum number of concurrent clients
|
%% Maximum number of concurrent clients
|
||||||
{max_clients, 1024},
|
{max_clients, 512},
|
||||||
%% Socket Access Control
|
%% Socket Access Control
|
||||||
{access, [{allow, all}]},
|
{access, [{allow, all}]},
|
||||||
%% SSL certificate and key files
|
%% SSL certificate and key files
|
||||||
|
@ -226,7 +226,7 @@
|
||||||
%% Size of acceptor pool
|
%% Size of acceptor pool
|
||||||
{acceptors, 4},
|
{acceptors, 4},
|
||||||
%% Maximum number of concurrent clients
|
%% Maximum number of concurrent clients
|
||||||
{max_clients, 512},
|
{max_clients, 64},
|
||||||
%% Socket Access Control
|
%% Socket Access Control
|
||||||
{access, [{allow, all}]},
|
{access, [{allow, all}]},
|
||||||
%% Socket Options
|
%% Socket Options
|
||||||
|
|
|
@ -22,10 +22,10 @@
|
||||||
|
|
||||||
## Enable kernel poll and a few async threads
|
## Enable kernel poll and a few async threads
|
||||||
+K true
|
+K true
|
||||||
+A 32
|
+A 16
|
||||||
|
|
||||||
## max process numbers
|
## max process numbers
|
||||||
+P 1000000
|
+P 8192
|
||||||
|
|
||||||
##-------------------------------------------------------------------------
|
##-------------------------------------------------------------------------
|
||||||
## Env
|
## Env
|
||||||
|
@ -36,8 +36,7 @@
|
||||||
|
|
||||||
-env ERTS_MAX_PORTS 4096
|
-env ERTS_MAX_PORTS 4096
|
||||||
|
|
||||||
#-env ERL_MAX_ETS_TABLES 1024
|
-env ERL_MAX_ETS_TABLES 1024
|
||||||
|
|
||||||
## Tweak GC to run more often
|
## Tweak GC to run more often
|
||||||
##-env ERL_FULLSWEEP_AFTER 1000
|
-env ERL_FULLSWEEP_AFTER 1000
|
||||||
#
|
|
||||||
|
|
|
@ -58,7 +58,8 @@
|
||||||
|
|
||||||
-export([new/3, name/1,
|
-export([new/3, name/1,
|
||||||
is_empty/1, is_full/1,
|
is_empty/1, is_full/1,
|
||||||
len/1, in/2, out/1]).
|
len/1, max_len/1,
|
||||||
|
in/2, out/1]).
|
||||||
|
|
||||||
-define(LOW_WM, 0.2).
|
-define(LOW_WM, 0.2).
|
||||||
|
|
||||||
|
@ -108,6 +109,8 @@ is_full(_MQ) -> false.
|
||||||
|
|
||||||
len(#mqueue{len = Len}) -> Len.
|
len(#mqueue{len = Len}) -> Len.
|
||||||
|
|
||||||
|
max_len(#mqueue{max_len= MaxLen}) -> MaxLen.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% @doc Queue one message.
|
%% @doc Queue one message.
|
||||||
%% @end
|
%% @end
|
||||||
|
|
|
@ -232,6 +232,7 @@ init([CleanSess, ClientId, ClientPid]) ->
|
||||||
max_awaiting_rel = emqttd_opts:g(max_awaiting_rel, SessEnv),
|
max_awaiting_rel = emqttd_opts:g(max_awaiting_rel, SessEnv),
|
||||||
expired_after = emqttd_opts:g(expired_after, SessEnv) * 3600,
|
expired_after = emqttd_opts:g(expired_after, SessEnv) * 3600,
|
||||||
timestamp = os:timestamp()},
|
timestamp = os:timestamp()},
|
||||||
|
emqttd_sm:register_session(CleanSess, ClientId, info(Session)),
|
||||||
{ok, Session, hibernate}.
|
{ok, Session, hibernate}.
|
||||||
|
|
||||||
handle_call({subscribe, TopicTable0}, _From, Session = #session{client_id = ClientId,
|
handle_call({subscribe, TopicTable0}, _From, Session = #session{client_id = ClientId,
|
||||||
|
@ -510,8 +511,8 @@ handle_info(Info, Session = #session{client_id = ClientId}) ->
|
||||||
lager:critical("Session ~s received unexpected info: ~p", [ClientId, Info]),
|
lager:critical("Session ~s received unexpected info: ~p", [ClientId, Info]),
|
||||||
{noreply, Session}.
|
{noreply, Session}.
|
||||||
|
|
||||||
terminate(_Reason, _Session) ->
|
terminate(_Reason, #session{clean_sess = CleanSess, client_id = ClientId}) ->
|
||||||
ok.
|
emqttd_sm:unregister_session(CleanSess, ClientId).
|
||||||
|
|
||||||
code_change(_OldVsn, Session, _Extra) ->
|
code_change(_OldVsn, Session, _Extra) ->
|
||||||
{ok, Session}.
|
{ok, Session}.
|
||||||
|
@ -629,3 +630,20 @@ cancel_timer(Ref) ->
|
||||||
noreply(State) ->
|
noreply(State) ->
|
||||||
{noreply, State, hibernate}.
|
{noreply, State, hibernate}.
|
||||||
|
|
||||||
|
info(#session{subscriptions = Subscriptions,
|
||||||
|
inflight_queue = InflightQueue,
|
||||||
|
max_inflight = MaxInflight,
|
||||||
|
message_queue = MessageQueue,
|
||||||
|
awaiting_rel = AwaitingRel,
|
||||||
|
awaiting_ack = AwaitingAck,
|
||||||
|
awaiting_comp = AwaitingComp,
|
||||||
|
timestamp = CreatedAt}) ->
|
||||||
|
[{pid, self()}, {subscriptions, Subscriptions},
|
||||||
|
{max_inflight, MaxInflight},
|
||||||
|
{inflight_queue, lists:length(InflightQueue)},
|
||||||
|
{message_queue, emqttd_mqueue:len(MessageQueue)},
|
||||||
|
{awaiting_rel, maps:size(AwaitingRel)},
|
||||||
|
{awaiting_ack, maps:size(AwaitingAck)},
|
||||||
|
{awaiting_comp, maps:size(AwaitingComp)},
|
||||||
|
{created_at, CreatedAt}].
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,8 @@
|
||||||
|
|
||||||
-export([start_session/2, lookup_session/1]).
|
-export([start_session/2, lookup_session/1]).
|
||||||
|
|
||||||
|
-export([register_session/3, unregister_session/2]).
|
||||||
|
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
%% gen_server Function Exports
|
%% gen_server Function Exports
|
||||||
|
@ -108,6 +110,29 @@ lookup_session(ClientId) ->
|
||||||
[] -> undefined
|
[] -> undefined
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
%% @doc Register a session with info.
|
||||||
|
%% @end
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
-spec register_session(CleanSess, ClientId, Info) -> ok when
|
||||||
|
CleanSess :: boolean(),
|
||||||
|
ClientId :: binary(),
|
||||||
|
Info :: [tuple()].
|
||||||
|
register_session(CleanSess, ClientId, Info) ->
|
||||||
|
SM = gproc_pool:pick_worker(?SM_POOL, ClientId),
|
||||||
|
gen_server:cast(SM, {register, CleanSess, ClientId, Info}).
|
||||||
|
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
%% @doc Unregister a session.
|
||||||
|
%% @end
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
-spec unregister_session(CleanSess, ClientId) -> ok when
|
||||||
|
CleanSess :: boolean(),
|
||||||
|
ClientId :: binary().
|
||||||
|
unregister_session(CleanSess, ClientId) ->
|
||||||
|
SM = gproc_pool:pick_worker(?SM_POOL, ClientId),
|
||||||
|
gen_server:cast(SM, {unregister, CleanSess, ClientId}).
|
||||||
|
|
||||||
call(SM, Req) -> gen_server:call(SM, Req, infinity).
|
call(SM, Req) -> gen_server:call(SM, Req, infinity).
|
||||||
|
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
|
@ -144,7 +169,25 @@ handle_call({start_session, {true, ClientId, ClientPid}}, _From, State) ->
|
||||||
handle_call(_Request, _From, State) ->
|
handle_call(_Request, _From, State) ->
|
||||||
{reply, ok, State}.
|
{reply, ok, State}.
|
||||||
|
|
||||||
handle_cast(_Msg, State) ->
|
%% transient session
|
||||||
|
handle_cast({register, true, ClientId, Info}, State) ->
|
||||||
|
ets:insert(mqtt_transient_session, {ClientId, Info}),
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
|
handle_cast({register, false, ClientId, Info}, State) ->
|
||||||
|
ets:insert(mqtt_persistent_session, {ClientId, Info}),
|
||||||
|
{noreply, setstats(State)};
|
||||||
|
|
||||||
|
handle_cast({unregister, true, ClientId}, State) ->
|
||||||
|
ets:delete(mqtt_transient_session, ClientId),
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
|
handle_cast({unregister, false, ClientId}, State) ->
|
||||||
|
ets:delete(mqtt_persistent_session, ClientId),
|
||||||
|
{noreply, setstats(State)};
|
||||||
|
|
||||||
|
handle_cast(Msg, State) ->
|
||||||
|
lager:critical("Unexpected Msg: ~p", [Msg]),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
handle_info({'DOWN', _MRef, process, DownPid, _Reason}, State) ->
|
handle_info({'DOWN', _MRef, process, DownPid, _Reason}, State) ->
|
||||||
|
@ -276,7 +319,6 @@ remove_session(Session) ->
|
||||||
mnesia:delete_object(session, Session, write)
|
mnesia:delete_object(session, Session, write)
|
||||||
end).
|
end).
|
||||||
|
|
||||||
setstats(State = #state{statsfun = _StatsFun}) ->
|
setstats(State = #state{statsfun = StatsFun}) ->
|
||||||
State.
|
StatsFun(ets:info(mqtt_persistent_session, size)), State.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,7 @@ start_link() ->
|
||||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
|
init_session_ets(),
|
||||||
Schedulers = erlang:system_info(schedulers),
|
Schedulers = erlang:system_info(schedulers),
|
||||||
gproc_pool:new(emqttd_sm:pool(), hash, [{size, Schedulers}]),
|
gproc_pool:new(emqttd_sm:pool(), hash, [{size, Schedulers}]),
|
||||||
StatsFun = emqttd_stats:statsfun('sessions/count', 'sessions/max'),
|
StatsFun = emqttd_stats:statsfun('sessions/count', 'sessions/max'),
|
||||||
|
@ -55,3 +56,8 @@ init([]) ->
|
||||||
end, lists:seq(1, Schedulers)),
|
end, lists:seq(1, Schedulers)),
|
||||||
{ok, {{one_for_all, 10, 100}, Children}}.
|
{ok, {{one_for_all, 10, 100}, Children}}.
|
||||||
|
|
||||||
|
init_session_ets() ->
|
||||||
|
Tables = [mqtt_transient_session, mqtt_persistent_session],
|
||||||
|
Attrs = [ordered_set, named_table, public, {write_concurrency, true}],
|
||||||
|
lists:foreach(fun(Tab) -> ets:new(Tab, Attrs) end, Tables).
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue