Skip to main content

Posts

Showing posts from May, 2012

erlangs gen_fsm with queueing

OK no time for a full blow, polished blog post; When using gen_fsm I realized that often I want some sort of queueing. Two reasons promote this: to reduce the complexity caused by {States} x {Events}to implement some kind of asynchronous command execution processing model I usualle encounter both reasons and I ended up with two useful function that I copy into every gen_fsm I build: deq(State = #state{evtq = []}) -> {next_state, ready, State}; deq(State = #state{evtq = [Msg | T]}) -> ?MODULE:ready(Msg, State#state{evtq = T}). This requires one adds evtq as a field to the #state{} record. The function deq/1 will be used to work of events(one could say commands) until the queue is empty and finally rests in the state ready. This function is called at the point where one would return {next_state, ready, State} So instead of transitioning directly to ready we call the deq function which will process all defered events. On the other hand there is a function enq/2 whi…