Skip to content

Commit

Permalink
Duplicate subscribers cannot be monitored
Browse files Browse the repository at this point in the history
Checks if the new subscriber pid is already subscribed and do not add it to the list of subscribers more than once.
  • Loading branch information
rambocoder committed Sep 27, 2012
1 parent 6b435ad commit 2bc56e0
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/tinymq_channel_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ handle_call(_From, _, State) ->
{noreply, State}.

handle_cast({From, subscribe, 'now', Subscriber}, State) ->
NewSubscribers = [{erlang:monitor(process, Subscriber), Subscriber}|State#state.subscribers],
NewSubscribers = add_or_do_nothing(Subscriber, State#state.subscribers),
gen_server:reply(From, {ok, now_to_micro_seconds(erlang:now())}),
{noreply, purge_old_messages(State#state{ subscribers = NewSubscribers })};

Expand Down Expand Up @@ -114,5 +114,12 @@ pull_messages(Timestamp, Subscriber, State) ->
Subscriber ! {self(), Now, ReturnMessages},
{State#state.subscribers, Now};
_ ->
{[{erlang:monitor(process, Subscriber), Subscriber}|State#state.subscribers], Now}
{add_or_do_nothing(Subscriber, State#state.subscribers), Now}
end.

% Checks if the new subscriber pid already has a monitor
add_or_do_nothing(NewSubscriber, Subscribers) ->
case lists:any(fun({_Ref, Pid}) -> Pid == NewSubscriber end, Subscribers) of
true -> Subscribers;
false -> [{erlang:monitor(process, NewSubscriber), NewSubscriber} | Subscribers]
end.

0 comments on commit 2bc56e0

Please sign in to comment.