Created
August 10, 2011 17:46
-
-
Save Vagabond/1137595 to your computer and use it in GitHub Desktop.
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
diff --git a/src/poolboy.erl b/src/poolboy.erl | |
index 9b71104..386df02 100644 | |
--- a/src/poolboy.erl | |
+++ b/src/poolboy.erl | |
@@ -47,16 +47,16 @@ ready({checkin, Pid}, State) -> | |
ready(_Event, State) -> | |
{next_state, ready, State}. | |
-ready(checkout, {From, _}, #state{workers=Workers, worker_sup=Sup, | |
+ready(checkout, {FromPid, _} = From, #state{workers=Workers, worker_sup=Sup, | |
max_overflow=MaxOverflow}=State) -> | |
case queue:out(Workers) of | |
{{value, Pid}, Left} -> | |
- Ref = erlang:monitor(process, From), | |
+ Ref = erlang:monitor(process, FromPid), | |
Monitors = [{Pid, Ref} | State#state.monitors], | |
{reply, Pid, ready, State#state{workers=Left, | |
monitors=Monitors}}; | |
{empty, Empty} when MaxOverflow > 0 -> | |
- {Pid, Ref} = new_worker(Sup, From), | |
+ {Pid, Ref} = new_worker(Sup, FromPid), | |
Monitors = [{Pid, Ref} | State#state.monitors], | |
{reply, Pid, overflow, State#state{workers=Empty, | |
monitors=Monitors, | |
@@ -69,7 +69,7 @@ ready(checkout, {From, _}, #state{workers=Workers, worker_sup=Sup, | |
ready(_Event, _From, State) -> | |
{reply, ok, ready, State}. | |
-overflow({checkin, Pid}, #state{overflow=1}=State) -> | |
+overflow({checkin, Pid}, #state{overflow=O}=State) when O =< 1 -> | |
dismiss_worker(Pid), | |
{next_state, ready, State#state{overflow=0}}; | |
overflow({checkin, Pid}, #state{overflow=Overflow}=State) -> | |
@@ -91,14 +91,22 @@ overflow(checkout, {From, _}, #state{worker_sup=Sup, | |
overflow(_Event, _From, State) -> | |
{reply, ok, overflow, State}. | |
-full({checkin, Pid}, #state{waiting=Waiting}=State) -> | |
+full({checkin, Pid}, #state{waiting=Waiting, max_overflow=MO}=State) -> | |
case queue:out(Waiting) of | |
- {{value, From}, Left} -> | |
- Ref = erlang:monitor(process, From), | |
- Monitors = [{Pid, Ref} | State#state.monitors], | |
+ {{value, {FromPid, _} = From}, Left} -> | |
+ Ref = erlang:monitor(process, FromPid), | |
+ Monitors = [{FromPid, Ref} | State#state.monitors], | |
gen_fsm:reply(From, Pid), | |
{next_state, full, State#state{waiting=Left, | |
monitors=Monitors}}; | |
+ {empty, Empty} when MO < 1 -> | |
+ Workers = queue:in(Pid, State#state.workers), | |
+ Monitors = case lists:keytake(Pid, 1, State#state.monitors) of | |
+ {value, {_, Ref}, Left} -> erlang:demonitor(Ref), Left; | |
+ false -> [] | |
+ end, | |
+ {next_state, ready, State#state{waiting=Empty, workers=Workers, | |
+ monitors=Monitors}}; | |
{empty, Empty} -> | |
dismiss_worker(Pid), | |
{next_state, overflow, State#state{waiting=Empty}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment