Domanda

I am trying to develop a chat server in erlang, and i want to broadcast messages between a group of processes, i came out with three alternatives

  1. send every message to master process which sends the message to the rest of the group, it looks like a bottleneck to me !.
  2. use a global ets table which contains all the pids of the group (excessive copying).
  3. use upd multicast.
  4. pg module which is the same as 1.

what is the best approach , is there other alternatives ?

È stato utile?

Soluzione

Multi-casting in Erlang is unicasting to each recipient. There is no way to broadcast information other than that. Since you only have a small count of processes I don't think you will have any problems with copying at all. At least I wouldn't worry too much about it until it becomes the bottleneck of the system.

Don't underestimate keeping a process which works as the multicaster for a group. It may be a simple solution, although just keeping the pid()'s in an ETS table or using gproc for it may be rougly the same.

Worrying about excessive copying is probably not going to be fruitful in the beginning. A trick here could be to store the message as a large binary and then send it around. Then, as Marcelo writes, you will only get to pass around the reference.

The solution by Vance Shipley on linking is one I would avoid. Links are bi-directional and you will have to trap exits to get the exit message. It is probably better to maintain lifetime knowledge via the call erlang:monitor(process, Pid). You will need to do that since a process that leaves has to be removed from the group. Receiving a message of the form {'DOWN', ..., ...} will be a sign you need to take care of cleanup.

Altri suggerimenti

The more natural method would be to have each process aware of the pid() of each of the other processes it will communicate with. Keep [pid()] in process state and keep it current by using link/1 to cause {'EXIT', Pid, Reason} to be received when a process dies.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top