Hi all,
I’m using the pg module in a project to make use of its amazing distributed named process group feature, but I’m missing something: tagged processes ({Tag :: any(), Process :: pid()}
). This would be very useful to identify some particular processes within a given group.
For example, suppose some resource is controlled by two workers and one leader and this process set is added to a pg group. In this way, it’s easy to get the processes associated to a resource, but it’s hard to identify the leader process among the returned ones:
% Set up group
Scope = some_scope,
GroupId = 42,
Workers = get_pid(workers), % returns [pid(), pid()]
Leader = get_pid(leader), % returns pid()
pg:join(Scope, GroupId, [Leader | Workers]).
% Getting members
Members = pg:get_members(Scope, GroupId).
% [Pid1, Pid2, Pid3] -> which one is the group leader?
As a workaround, I’m creating another group with only the leader process, so I end up with two groups: one for workers and another one for leader (or one for all members and another one for leader). It would be the same if using scope for separating processes by tag/role: we would end up with single-element groups to accommodate such particular processes.
Also, this approach requires a new group per each label that is created. This seems to me I’m missing something.
One way to solve this question is to allow a tag to be associated with a process when joining the group. With tagged processes, these particular processes would be easily identified:
pg:join(Scope, GroupId, [{leader, Leader} | Workers]).
Members = pg:get_members(Scope, GroupId).
% something like [Pid1, {leader, Pid2}, Pid3]
% it's easy to get the leader process from the group
{leader, LeaderPid} = lists:keyfind(leader, 1, Members).
This approach seems to resolve the question without breaking backward compatibility or changing the module too much.
What do you think about this feature? Am I missing some way to use pg without tagged processes and being able to easily pick some particular processes from a group?
Finally, I’m working on a PR to add this feature to the pg module if this idea is accepted. Suggestions are welcome.
–
Best regards,
Paulo Zulato