Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #7 from rambocoder/master

Expire unused channels and only have one channel supervisor
  • Loading branch information...
commit 0d563ffe64936750dc48a7662192676cb9ec381a 2 parents a3fd2cf + 4c495b2
@evanmiller evanmiller authored
View
11 src/tinymq_channel_controller.erl
@@ -39,7 +39,7 @@ handle_cast({From, subscribe, Timestamp, Subscriber}, State) ->
{NewSubscribers, LastPull} = pull_messages(ActualTimestamp, Subscriber, State),
gen_server:reply(From, {ok, LastPull}),
{noreply, purge_old_messages(State#state{ subscribers = NewSubscribers,
- last_pull = LastPull})};
+ last_pull = LastPull}), State#state.max_age * 1000};
handle_cast({From, poll, Timestamp}, State) ->
ActualTimestamp = case Timestamp of
@@ -50,7 +50,7 @@ handle_cast({From, poll, Timestamp}, State) ->
ReturnMessages = messages_newer_than_timestamp(ActualTimestamp, State#state.messages),
Now = now_to_micro_seconds(erlang:now()),
gen_server:reply(From, {ok, Now, ReturnMessages}),
- {noreply, purge_old_messages(State#state{ last_pull = Now })};
+ {noreply, purge_old_messages(State#state{ last_pull = Now }), State#state.max_age * 1000};
handle_cast({From, push, Message}, State) ->
Now = now_to_micro_seconds(erlang:now()),
@@ -62,11 +62,11 @@ handle_cast({From, push, Message}, State) ->
gen_server:reply(From, {ok, Now}),
State2 = purge_old_messages(State),
NewMessages = tiny_pq:insert_value(Now, Message, State2#state.messages),
- {noreply, State2#state{messages = NewMessages, subscribers = [], last_pull = LastPull}};
+ {noreply, State2#state{messages = NewMessages, subscribers = [], last_pull = LastPull}, State#state.max_age * 1000};
handle_cast({From, now}, State) ->
gen_server:reply(From, now_to_micro_seconds(erlang:now())),
- {noreply, purge_old_messages(State)}.
+ {noreply, purge_old_messages(State), State#state.max_age * 1000}.
terminate(_Reason, _State) ->
ok.
@@ -76,8 +76,7 @@ code_change(_OldVsn, State, _Extra) ->
handle_info(timeout, State) ->
gen_server:cast(tinymq, {expire, State#state.channel}),
- exit(State#state.supervisor),
- {noreply, State};
+ {stop, normal, State};
handle_info({'DOWN', Ref, process, _Pid, _Reason}, State) ->
{noreply, State#state{ subscribers = proplists:delete(Ref, State#state.subscribers) }};
handle_info(_Info, State) ->
View
8 src/tinymq_channel_sup.erl
@@ -5,14 +5,14 @@
-export([start_link/0, start_link/1, init/1]).
start_link() ->
- supervisor:start_link(?MODULE, []).
+ supervisor:start_link({local, tinymq_channel_sup}, ?MODULE, []).
start_link(StartArgs) ->
- supervisor:start_link(?MODULE, StartArgs).
+ supervisor:start_link({local, tinymq_channel_sup}, ?MODULE, StartArgs).
init(_StartArgs) ->
- {ok, {{simple_one_for_one, 10, 10},
+ {ok, {{simple_one_for_one, 0, 10},
[
{mq_channel_controller, {tinymq_channel_controller, start_link, []},
- permanent, 2000, worker, [tinymq_channel_controller]}
+ temporary, 2000, worker, [tinymq_channel_controller]}
]}}.
View
3  src/tinymq_controller.erl
@@ -63,8 +63,7 @@ find_or_create_channel(Channel, #state{dict = Chan2Pid, max_age = MaxAge} = Stat
{ok, Pid} ->
{Pid, State};
_ ->
- {ok, ChannelSup} = tinymq_channel_sup:start_link(),
- {ok, ChannelPid} = supervisor:start_child(ChannelSup, [MaxAge, ChannelSup, Channel]),
+ {ok, ChannelPid} = supervisor:start_child(tinymq_channel_sup, [MaxAge, tinymq_channel_sup, Channel]),
{ChannelPid, State#state{
dict = dict:store(Channel, ChannelPid, Chan2Pid)
}}
View
16 src/tinymq_sup.erl
@@ -10,10 +10,12 @@ start_link() ->
supervisor:start_link(?MODULE, []).
init([]) ->
- {ok, {{one_for_one, 10, 10}, [
- {mq_controller, {tinymq_controller, start_link, []},
- permanent,
- 2000,
- worker,
- [tinymq_controller]}
- ]}}.
+ MqWorker = {mq_controller, {tinymq_controller, start_link, []},
+ permanent, 2000, worker, [tinymq_controller]},
+
+ ChannelSup = {tinymq_channel_sup, {tinymq_channel_sup, start_link, []},
+ permanent, 2000, supervisor, [tinymq_channel_sup]},
+
+ Children = [MqWorker, ChannelSup],
+ RestartStrategy = {one_for_one, 10, 10},
+ {ok, {RestartStrategy, Children}}.

0 comments on commit 0d563ff

Please sign in to comment.
Something went wrong with that request. Please try again.