[erlang-questions] How to put together gproc, hundreds of thousand of processes, timeouts?

Max Bourinov bourinov@REDACTED
Thu Nov 10 07:36:17 CET 2011


Hello Erlangers!

I am developing back-end system which must be capable to serve many
thousands of users and do the following:

1. For each connected user it must create a separate gen_server process.
2. When any user interacts with the system its process gets messages.
3. When a user is idle for some while (5 minutes) its process triggers a
timeout event.

So, all my processes lives in gproc. They are getting messages
and everything seems to be fine.

I would like to know what is the best way to implement timeouts?

I have two approaches. Each has advantages and disadvantages and your
feedback is very welcome!

*Approach 1: (How it works now)*
For each user process I start a timer:

    timer:send_interval(60000, {tik}), % Every minute send me a {tik}
message

In the process state I am counting tiks. If a message from user comes I
reset the tik counter. If the tik counter reaches certain value it triggers
timeout event.

Advantages: There is only one(?) timer process.

Disadvantages: I expect high load on ETS when there is a high traffic.

*Approach 2:*
In each user process instead of using timer I do the following:

    Self = self(),
    Fun = fun(ThisFun) -> Self ! {tik}, receive after 1000 -> nil end,
ThisFun(ThisFun) end,
    spawn(fun() -> Fun(Fun) end),

Basically this is the same but here I have as twice as much of processes
and it runs without ETS. (of course gproc uses ETS for its needs).

Any ideas which one will do work better?

Max
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111110/309e1f3e/attachment.htm>


More information about the erlang-questions mailing list