Skip to content

Commit

Permalink
Correctly service queued work on worker death while full
Browse files Browse the repository at this point in the history
Also, correctly restart workers when appropriate
  • Loading branch information
Vagabond committed Aug 12, 2011
1 parent 0392b05 commit 4c4ff9a
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/poolboy.erl
Expand Up @@ -148,6 +148,7 @@ handle_info({'DOWN', Ref, _, _, _}, StateName, State) ->
{next_state, StateName, State};
handle_info({'EXIT', Pid, _}, StateName, #state{worker_sup=Sup,
overflow=Overflow,
waiting=Waiting,
max_overflow=MaxOverflow}=State) ->
Monitors = case lists:keytake(Pid, 1, State#state.monitors) of
{value, {_, Ref}, Left} -> erlang:demonitor(Ref), Left;
Expand All @@ -163,9 +164,32 @@ handle_info({'EXIT', Pid, _}, StateName, #state{worker_sup=Sup,
overflow ->
{next_state, overflow, State#state{monitors=Monitors,
overflow=Overflow-1}};
full when MaxOverflow < 1 ->
case queue:out(Waiting) of
{{value, {FromPid, _} = From}, LeftWaiting} ->
MonitorRef = erlang:monitor(process, FromPid),
Monitors2 = [{FromPid, MonitorRef} | Monitors],
NewWorker = new_worker(Sup),
gen_fsm:reply(From, NewWorker),
{next_state, full, State#state{waiting=LeftWaiting,
monitors=Monitors2}};
{empty, Empty} ->
{next_state, ready, State#state{monitors=Monitors,waiting=Empty,
workers=queue:in(new_worker(Sup), State#state.workers)}}
end;
full when Overflow =< MaxOverflow ->
{next_state, overflow, State#state{monitors=Monitors,
overflow=Overflow-1}};
case queue:out(Waiting) of
{{value, {FromPid, _} = From}, LeftWaiting} ->
MonitorRef = erlang:monitor(process, FromPid),
Monitors2 = [{FromPid, MonitorRef} | Monitors],
NewWorker = new_worker(Sup),
gen_fsm:reply(From, NewWorker),
{next_state, full, State#state{waiting=LeftWaiting,
monitors=Monitors2}};
{empty, Empty} ->
{next_state, overflow, State#state{monitors=Monitors,
overflow=Overflow-1, waiting=Empty}}
end;
full ->
{next_state, full, State#state{monitors=Monitors,
overflow=Overflow-1}}
Expand Down

0 comments on commit 4c4ff9a

Please sign in to comment.