Hello Erlangers!<div><br></div><div>I am developing back-end system which must be capable to serve many thousands of users and do the following:</div><div><br></div><div>1. For each connected user it must create a separate gen_server process.</div>
<div>2. When any user interacts with the system its process gets messages.</div><div>3. When a user is idle for some while (5 minutes) its process triggers a timeout event.</div><div><br></div><div>So, all my processes lives in gproc. They are getting messages and everything seems to be fine.</div>
<div><br></div><div>I would like to know what is the best way to implement timeouts?</div><div><br></div><div>I have two approaches. Each has advantages and disadvantages and your feedback is very welcome!</div><div><br>
</div>
<div><b>Approach 1: (How it works now)</b></div><div>For each user process I start a timer:</div><div><br></div><div> <font class="Apple-style-span" face="'courier new', monospace">timer:send_interval(60000, {tik}), % Every minute send me a {tik} message</font></div>
<div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">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.</font></div>
<div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">Advantages: There is only one(?) timer process.</font></div>
<div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">Disadvantages: I expect high load on ETS when there is a high traffic.</font></div>
<div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><b>Approach 2:</b></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">In each user process instead of using timer I do the following:</font></div>
<div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div><div><div><font class="Apple-style-span" face="'courier new', monospace"> Self = self(),</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> Fun = fun(ThisFun) -> Self ! {tik}, receive after 1000 -> nil end, ThisFun(ThisFun) end,</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> spawn(fun() -> Fun(Fun) end),</font></div></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div>
<div>
<font class="Apple-style-span" face="arial, helvetica, sans-serif">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).</font></div><div>
<font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">Any ideas which one will do work better?</font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br>
</font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">Max</font></div>