Skip to content

Instantly share code, notes, and snippets.

@jadeallenx
Created December 11, 2013 05:56
Show Gist options
  • Save jadeallenx/7905693 to your computer and use it in GitHub Desktop.
Save jadeallenx/7905693 to your computer and use it in GitHub Desktop.
Aggregator prototype
% By Grabthar's Hammer you will be avenged.
-module(grabthar).
-export([start_link/3]).
-record(state, {
customer_id,
timer,
threshold,
current = 1,
pending = queue:new()
}).
start_link(CustomerId, Threshold, Timeout) ->
Pid = spawn_link(fun() ->
Tref = erlang:send_after(Timeout, self(), release_pending),
loop(#state{threshold=Threshold, timer=Tref, customer_id = CustomerId})
end),
{ok, Pid}.
loop(State) ->
receive
{add_notification, N} ->
NewState = case State#state.current > State#state.threshold of
true ->
State#state{ pending = queue:in(N, State#state.pending) };
false ->
error_logger:format("~p", [N]),
State#state{ current = State#state.current + 1 }
end,
loop(NewState);
time_remaining ->
error_logger:format("~p", [erlang:read_timer(State#state.timer)]),
loop(State);
queue_len ->
error_logger:format("~p", [queue:len(State#state.pending)]),
loop(State);
release_pending ->
error_logger:format("~p", [queue:to_list(State#state.pending)])
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment