Created
December 11, 2013 05:56
-
-
Save jadeallenx/7905693 to your computer and use it in GitHub Desktop.
Aggregator prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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