[erlang-questions] gen_server aggregating calls/casts

Jonathan Leivent jleivent@REDACTED
Mon Jul 8 21:31:45 CEST 2013


I would like the server process I'm designing to be able to aggregate 
certain calls and/or casts - but I don't see any way to do this with 
gen_server.

Specifically, when this server is within a particular handle_call, it 
should find all similar calls on its message queue and handle them 
together, responding to all of the callers at once after performing a 
single combined action on their behalf.  It can't accumulate calls into 
its state because there are other unrelated calls/casts it can encounter 
prior to seeing all of the currently queued matching calls. 
Conceptually, it needs to do something like:

drain_all_requests(List) ->
   receive
     R={request, _} -> drain_all_requests([R|List])
   after 0 -> List
   end.

handle_call(R={request, Info}, From, State) ->
   RequestList = drain_all_requests([R]),
   handle_aggregated_requests(RequestList, State),
   ...

However, I can't write the necessary receive to drain the message queue 
of the matching calls I'd like to aggregate - because I don't know the 
internal format of message wrappers used by gen_server.  And I don't 
have a way to respond to any but the one that put the server into the 
handle_call.  I would be willing to use handle_cast instead (and roll my 
own response scheme), but I still don't have a way to receive the 
matching casts on the message queue.

I guess I could examine the source code for gen_server and determine the 
call and/or cast message format, then use that.  But, I'd rather play by 
the modularity rules.  Is there any way I can do that?

Thanks
-- Jonathan



More information about the erlang-questions mailing list