Skip to content

Commit

Permalink
asynchronising imem_server calls #276
Browse files Browse the repository at this point in the history
  • Loading branch information
shamis committed Aug 2, 2019
1 parent ad30c6b commit 1d6987e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
18 changes: 16 additions & 2 deletions src/imem_seco.erl
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ seco_existing(SKey) ->
case if_read(ddSeCo@, SKey) of
[#ddSeCo{pid=Pid} = SeCo] when Pid == self() ->
SeCo;
[#ddSeCo{pid=Pid} = SeCo] ->
{links, Links} = erlang:process_info(self(), links),
case lists:member(Pid, Links) of
true ->
SeCo;
false ->
?SecurityViolation({"Not logged in", SKey})
end;
[] ->
?SecurityException({"Not logged in", SKey})
end.
Expand All @@ -379,8 +387,14 @@ seco_authorized(SKey) ->
case if_read(ddSeCo@, SKey) of
[#ddSeCo{pid=Pid, authState=authorized} = SeCo] when Pid == self() ->
SeCo;
[#ddSeCo{}] ->
?SecurityViolation({"Not logged in", SKey});
[#ddSeCo{pid=Pid, authState=authorized} = SeCo] ->
{links, Links} = erlang:process_info(self(), links),
case lists:member(Pid, Links) of
true ->
SeCo;
false ->
?SecurityViolation({"Not logged in", SKey})
end;
[] ->
?SecurityException({"Not logged in", SKey})
end.
Expand Down
28 changes: 21 additions & 7 deletions src/imem_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,32 @@ mfa({Ref, Mod, which_applications, Args}, Transport) when Mod =:= imem_sec;
mfa({_Ref, imem_sec, echo, [_, Term]}, Transport) ->
send_resp({server_echo, Term}, Transport),
ok;
mfa({Ref, imem_sec = Mod, Fun, Args}, Transport) when Fun =:= login;
Fun =:= auth_start;
Fun =:= schema;
Fun =:= logout;
Fun =:= auth_add_cred ->
mfa(Ref, Mod, Fun, Args, Transport);
mfa({Ref, Mod, Fun, Args}, Transport) ->
% mfa(Ref, Mod, Fun, Args);
spawn_link(fun() ->
mfa(Ref, Mod, Fun, Args, Transport)
end),
ok. % 'ok' returned for erlimem compatibility

mfa(Ref, Mod, Fun, Args, Transport) ->
NewArgs = args(Ref,Fun,Args,Transport),
ApplyRes = try
?TLog("~p MFA -> R ~n ~p:~p(~p)~n", [Transport,Mod,Fun,NewArgs]),
apply(Mod,Fun,NewArgs)
catch
_Class:Reason -> {error, {Reason, erlang:get_stacktrace()}}
end,
ApplyRes =
try
?TLog("~p MFA -> R ~n ~p:~p(~p)~n", [Transport,Mod,Fun,NewArgs]),
apply(Mod,Fun,NewArgs)
catch
_Class:Reason -> {error, {Reason, erlang:get_stacktrace()}}
end,
?TLog("~p MFA -> R ~n ~p:~p(~p) -> ~p~n", [Transport,Mod,Fun,NewArgs,ApplyRes]),
?TLog("~p MF -> R ~n ~p:~p -> ~p~n", [Transport,Mod,Fun,ApplyRes]),
send_resp(ApplyRes, Transport),
ok. % 'ok' returned for erlimem compatibility
ok.

args(R, fetch_recs_async, A, {_,_,R} = T) ->
Args = lists:sublist(A, length(A)-1) ++ [T],
Expand Down

0 comments on commit 1d6987e

Please sign in to comment.