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.
-spec open(clientinfo(), conninfo(), emqx_session:conf()) ->
{true, session(), []} | {false, session()}.
{_IsPresent :: true, session(), []} | {_IsPresent :: false, session()}.
open(#{clientid := ClientID}, _ConnInfo, Conf) ->
% NOTE
% 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.
ok = emqx_cm:discard_session(ClientID),
{IsNew, Session} = emqx_ds:session_open(ClientID, Conf),
case IsNew of
false ->
{true, Session, []};
IsPresent = not IsNew,
case IsPresent of
true ->
{false, Session}
{IsPresent, Session, []};
false ->
{IsPresent, Session}
end.
-spec destroy(session() | clientinfo()) -> ok.

View File

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

View File

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

View File

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