refactor(session): make typespecsa and flow a bit more clear

Co-Authored-By: Thales Macedo Garitezi <thalesmg@gmail.com>
This commit is contained in:
Andrew Mayorov 2023-09-19 12:12:54 +04:00
parent e422f492ef
commit adc29e15cc
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
4 changed files with 15 additions and 13 deletions

View File

@ -102,7 +102,7 @@ create(#{clientid := ClientID}, _ConnInfo, Conf) ->
Session. Session.
-spec open(clientinfo(), conninfo(), emqx_session:conf()) -> -spec open(clientinfo(), conninfo(), emqx_session:conf()) ->
{true, session(), []} | {false, session()}. {_IsPresent :: true, session(), []} | {_IsPresent :: false, session()}.
open(#{clientid := ClientID}, _ConnInfo, Conf) -> open(#{clientid := ClientID}, _ConnInfo, Conf) ->
% NOTE % NOTE
% The fact that we need to concern about discarding all live channels here % The fact that we need to concern about discarding all live channels here
@ -112,11 +112,12 @@ open(#{clientid := ClientID}, _ConnInfo, Conf) ->
% space, and move this call back into `emqx_cm` where it belongs. % space, and move this call back into `emqx_cm` where it belongs.
ok = emqx_cm:discard_session(ClientID), ok = emqx_cm:discard_session(ClientID),
{IsNew, Session} = emqx_ds:session_open(ClientID, Conf), {IsNew, Session} = emqx_ds:session_open(ClientID, Conf),
case IsNew of IsPresent = not IsNew,
false -> case IsPresent of
{true, Session, []};
true -> true ->
{false, Session} {IsPresent, Session, []};
false ->
{IsPresent, Session}
end. end.
-spec destroy(session() | clientinfo()) -> ok. -spec destroy(session() | clientinfo()) -> ok.

View File

@ -172,17 +172,18 @@ create(ClientInfo, ConnInfo, Conf) ->
ok = emqx_hooks:run('session.created', [ClientInfo, info(Session)]), ok = emqx_hooks:run('session.created', [ClientInfo, info(Session)]),
Session. Session.
-spec open(clientinfo(), conninfo()) -> {true, t(), _ReplayContext} | {false, t()}. -spec open(clientinfo(), conninfo()) ->
{_IsPresent :: true, t(), _ReplayContext} | {_IsPresent :: false, t()}.
open(ClientInfo, ConnInfo) -> open(ClientInfo, ConnInfo) ->
Conf = get_session_conf(ClientInfo, ConnInfo), Conf = get_session_conf(ClientInfo, ConnInfo),
case (choose_impl_mod(ConnInfo)):open(ClientInfo, ConnInfo, Conf) of case (choose_impl_mod(ConnInfo)):open(ClientInfo, ConnInfo, Conf) of
{true, Session, ReplayContext} -> {_IsPresent = true, Session, ReplayContext} ->
{true, Session, ReplayContext}; {true, Session, ReplayContext};
{false, Session} -> {_IsPresent = false, NewSession} ->
ok = emqx_metrics:inc('session.created'), ok = emqx_metrics:inc('session.created'),
ok = emqx_hooks:run('session.created', [ClientInfo, info(Session)]), ok = emqx_hooks:run('session.created', [ClientInfo, info(NewSession)]),
{false, Session}; {false, NewSession};
false -> _IsPresent = false ->
{false, create(ClientInfo, ConnInfo, Conf)} {false, create(ClientInfo, ConnInfo, Conf)}
end. end.

View File

@ -194,7 +194,7 @@ destroy(_Session) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec open(clientinfo(), conninfo(), emqx_session:conf()) -> -spec open(clientinfo(), conninfo(), emqx_session:conf()) ->
{true, session(), replayctx()} | false. {_IsPresent :: true, session(), replayctx()} | _IsPresent :: false.
open(ClientInfo = #{clientid := ClientId}, _ConnInfo, _Conf) -> open(ClientInfo = #{clientid := ClientId}, _ConnInfo, _Conf) ->
case emqx_cm:takeover_session_begin(ClientId) of case emqx_cm:takeover_session_begin(ClientId) of
{ok, SessionRemote, TakeoverState} -> {ok, SessionRemote, TakeoverState} ->

View File

@ -392,7 +392,7 @@ t_takeover_session(_) ->
gen_server:reply(From1, test), gen_server:reply(From1, test),
receive receive
{'$gen_call', From2, {takeover, 'end'}} -> {'$gen_call', From2, {takeover, 'end'}} ->
gen_server:reply(From2, []) gen_server:reply(From2, _Pendings = [])
end end
end end
end), end),