Просмотр исходного кода

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

Co-Authored-By: Thales Macedo Garitezi <thalesmg@gmail.com>
Andrew Mayorov 2 лет назад
Родитель
Сommit
adc29e15cc

+ 6 - 5
apps/emqx/src/emqx_persistent_session_ds.erl

@@ -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.

+ 7 - 6
apps/emqx/src/emqx_session.erl

@@ -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.
 

+ 1 - 1
apps/emqx/src/emqx_session_mem.erl

@@ -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} ->

+ 1 - 1
apps/emqx/test/emqx_cm_SUITE.erl

@@ -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),