Skip to content

Instantly share code, notes, and snippets.

@jamii
Created January 23, 2012 16:20
Show Gist options
  • Save jamii/1664066 to your computer and use it in GitHub Desktop.
Save jamii/1664066 to your computer and use it in GitHub Desktop.
in_journal() ->
receive
{Seq, Event} ->
transaction_manager ! {Seq, Event},
write({Seq, Event}),
sync(),
out_journal ! {journaled, Seq}
end.
% in cpp or ocaml
transaction_manager() ->
receive
{Seq, Event} ->
{Result, OutEvents} = handle(Event),
out_jornal ! {handled, Seq, Result, OutEvents}
end.
out_journal() ->
receive
{handled, Seq, Result, OutEvents} ->
receive
{journaled, Seq} ->
write({Seq, started}),
sync(),
send(OutEvents),
write({Seq, finished}),
sync()
end
end.
% on recovery, have to manually check any unfinished output
% worth batching writes so we don't have to sync as often eg
receive_batch(Acc, 0) ->
Acc;
receive_batch(Acc, Max) ->
receive
Event ->
receive_batch([Event|Acc], Max-1)
after 0 ->
Acc
end.
in_journal() ->
Events = receive_batch([], 16),
transaction_manager ! Events
write(Events),
sync(),
out_journal ! {journaled_up_to, max_seq(Events)}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment