Reply To: gen_server get last message in mailbox

Home Forums OTP Questions gen_server get last message in mailbox Reply To: gen_server get last message in mailbox

#46284

zxq9
Member

In this case I would recommend process A accumulate a list of jobs, and flush them to a temporary process B to execute the list every X seconds. Upon completion of its task list, B will die (poof!). If A gives a B too much work to complete before the next B is created, it now doesn’t matter because they run in parallel anyway. If you want a crash of a B to kill the whole system, the spawn_link it, if you just want to watch it, spawn_monitor, if you’re indifferent, spawn.

%% A simple aggregator
aggregate(ToDo, Tick) ->
  receive
    {new_work, Z} ->
        aggregate([Z | ToDo]);
    flush ->
        spawn_link(fun() -> lists:map(fun process/1, ToDo)),
        timer:send_after(Tick, flush),
        aggregate([])
  end.

%% Some work you want done
process(Item) -> 
    % whatever you need

Note that the aggregator above does not implement any pushback, throttling, or whatever. If you have a high volume system or are running into performance wonkiness you will want to come up with a way for a node to calibrate itself to its best throughput and push back on the workload (or shed load) and send an operator a notification that its maxed out if the volume exceeds whatever you set the max to.

Hopefully I explained that adequately… :-)

If there is some more interesting reason why this doesn’t address the issue, explain a bit further and we’ll probably iron it out.