[erlang-questions] gproc and synchronous start of registred processes

Magnus Klaar magnus.klaar@REDACTED
Mon Oct 31 20:21:44 CET 2011


It's worth taking a closer look at the supervisor behavior, all restart
strategies except the simple_one_for_one strategy allows you specify a
unique id for all child processes added to the supervisor. If a duplicate
process with the same id is added at a later point in time an error
containing the pid of the existing child process is returned. Ideally, you
would register it under the same id in gproc to make future lookups and
inspection easier.

Your existing approach isn't half bad to be honest, you don't need to tear
it _all_ down because you change where you store the process names. If you
need to replace this server with a more complex code to handle the race
condition you created you're not saving yourself any trouble. You can keep
the server for synchronization but change the data store.

MVH Magnus

On Mon, Oct 31, 2011 at 1:32 PM, Max Lapshin <max.lapshin@REDACTED> wrote:

> Browser makes two requests: A, B.
> Request A comes to http server and looks if there is
> gproc:lookup_local_name(my_proc), it is undefined.
> Request B comes to http server and looks if there is
> gproc:lookup_local_name(my_proc), it is undefined.
> Request A starts new process my_proc:start_link  (lets call it
> pid(0,100,0) ) and waits while its init returns
> Request B starts new process my_proc:start_link  (lets call it
> pid(0,101,0) ) and waits while its init returns
> pid(0,100,0): calls gproc:add_local_name(my_proc) and let process A
> return with success
> pid(0,101,0): calls gproc:add_local_name(my_proc) and fails with gproc
> error.
> Request A returns with success
> Request B returns with failure, but nothing bad has really happened on
> server.
> Now lets speak more concrete. 5000 of flash clients are waiting for
> live event beginning and all of them are rechecking if stream has
> started.
> While there is no stream, nothing bad is happening, because they are
> making not more than 5000 of success requests per several seconds, it
> is ok.
> But when stream appears, only one will return with success, all other
> will log 4999 failures into error log. Funny, really?
> I've solved it with the following code:
> Check for registered name outside of registrator process:
> https://github.com/maxlapshin/game2/blob/master/src/game_tracker.erl#L44
> and if no name found, ask registrator process for doing it:
> https://github.com/maxlapshin/game2/blob/master/src/game_tracker.erl#L89
> Question is: maybe I've missed something and there is something like:
> gproc:add_local_name(my_proc, fun() ->
>   my_proc:start_link(..)
> end),
> which will do it synchronously?
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111031/d8938687/attachment.htm>

More information about the erlang-questions mailing list