New process for call_controller_action()

49 views
Skip to first unread message

tome...@gmail.com

unread,
Jan 24, 2014, 9:43:47 AM1/24/14
to chica...@googlegroups.com
Hi!

I have a quick question:
in boss_web_controller:call_controller_action, there is new process spawned,
just to do job and and send result back to parent.

call_controller_action(Adapter, AdapterInfo, RequestContext) ->
    lager:notice("Calling Controller Adapter ~s", [Adapter]),
    process_flag(trap_exit, true),
    Ref                = make_ref(),
    CHandlerPid = self(),
    _N = spawn_link(fun() ->
                         R = Adapter:action(AdapterInfo, RequestContext),
                         CHandlerPid !{msg,Ref, R}
         end),
    receive_controller_response(Ref).

-spec(receive_controller_response(reference()) ->any()).
receive_controller_response(Ref) ->
    receive
        {msg, Ref, R} ->
         lager:notice("Response ~p", [R]),
            R;

        {'EXIT',_From, normal} ->
            %%lager:error("2 Controller Process Exited normal ~p but response not yet receive", [From]),
            receive_controller_response(Ref);

        {'EXIT',From, Reason} ->
            lager:error("Controller Process Exited ~p ~p", [From, Reason]),
            {output, "Process Error see console.log for details\n"}
    end.

Is there some reason for that?
It is synchronous, so it could be replaced with:

call_controller_action(Adapter, AdapterInfo, RequestContext) ->
Adapter:action(AdapterInfo, RequestContext).

Or maybe surround the call in try ... catch.
If there is no reason for spawning new process, than I'll make a pull request,
if there is - I would really like to know :)

Cuong Thai

unread,
Jan 24, 2014, 7:46:45 PM1/24/14
to chica...@googlegroups.com, tome...@gmail.com
Hi,

The original call_controller_action is asynchronous. We call it and don't know when Adapter:action/2 is called, then go next function.
Your suggestion is synchronous. You go next function, only after Adapter:action/2 has called.

Regards,
Cuong Th.

tome...@gmail.com

unread,
Jan 25, 2014, 5:23:24 PM1/25/14
to chica...@googlegroups.com, tome...@gmail.com
The original solutions is also synchronous,
because as you wrote:
- we call it and don't know when Adapter:action/2 is called,
- then we DON'T go to the next function - we are waiting for the response.
There is not a single action between spawning new process and waiting for it to return,
which makes it synchronous.

It is wasteful to spawn process to do the synchronous job.
As I can see in the file history, it also introduced a bug,
where parent process received {'EXIT', normal} before actual message.
It is cool to know, that this is possible!

Cuong Thai

unread,
Jan 26, 2014, 10:15:04 AM1/26/14
to chica...@googlegroups.com, tome...@gmail.com
You're right!
spawn_link doesn't help here.
Reply all
Reply to author
Forward
0 new messages