pg2...a warning

Evans, Matthew <>
Wed Apr 28 23:51:33 CEST 2010


Hi,

We have a specific configuration where there are 2 special hardware nodes that perform a specific task. We have an instance of a gen_server process on each of these nodes that acts as a gateway for other processes to request this task. This request is done by doing a call to pg2:get_closest_pid/1.

The problem is that an asynchronous operation, beyond our control, can cause pg2:join/2 to be called many times for the same process. The result of which is that pg2:get_closest_pid/1 will not be random (e.g. process on node 1 gets 5 "joins", and node 2 gets 3 "joins"). Or rather it will not be random in how we consider it to be (i.e. we only want a process to join a group a single time).

The documentation implies that this is the case:

join(Name, Pid) -> ok | {error, Reason}

Types:
Name = term()
Pid = pid()
Reason = {no_such_group, Name}

Joins the process Pid to the group Name. A process can join a group several times; it must then leave the group the same number of times.

We solved this by doing:

            Self = self(),
            ok = case lists:member(Self, pg2:get_members(?MODULE)) of
                true -> ok;
                _ -> pg2:join(?MODULE, Self)
            end;

To ensure we join only once.

I'm sure this is intentional, but perhaps the documentation should be more specific? Or even have an option to allow only a single join.

Regards

Matt


More information about the erlang-questions mailing list