summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Radestock <matthias@lshift.net>2008-12-18 21:28:57 +0000
committerMatthias Radestock <matthias@lshift.net>2008-12-18 21:28:57 +0000
commit13cc87c8935cdabf65fded6015f02b5991f37204 (patch)
tree52b816281c6bec69190b8bf6635c543a8ee2b54c
parent31b713c6de94dae6329fbbf12aa1f3d4af0f2ba4 (diff)
downloadrabbitmq-server-13cc87c8935cdabf65fded6015f02b5991f37204.tar.gz
refactoring
-rw-r--r--src/rabbit_limiter.erl32
1 files changed, 14 insertions, 18 deletions
diff --git a/src/rabbit_limiter.erl b/src/rabbit_limiter.erl
index cd8f7734..6388c360 100644
--- a/src/rabbit_limiter.erl
+++ b/src/rabbit_limiter.erl
@@ -89,19 +89,14 @@ handle_call({can_send, QPid}, _From, State = #lim{in_use = InUse}) ->
false -> {reply, true, State#lim{in_use = InUse + 1}}
end.
-handle_cast({prefetch_count, PrefetchCount},
- State = #lim{prefetch_count = CurrentLimit}) ->
- NewState = State#lim{prefetch_count = PrefetchCount},
- {noreply, if PrefetchCount > CurrentLimit -> forget_queues(NewState);
- true -> NewState
- end};
-
-handle_cast({decrement_capacity, Magnitude}, State) ->
- NewState = decrement_in_use(Magnitude, State),
- ShouldNotify = limit_reached(State) and not(limit_reached(NewState)),
- {noreply, if ShouldNotify -> forget_queues(NewState);
- true -> NewState
- end}.
+handle_cast({prefetch_count, PrefetchCount}, State) ->
+ {noreply, maybe_notify(State, State#lim{prefetch_count = PrefetchCount})};
+
+handle_cast({decrement_capacity, Magnitude}, State = #lim{in_use = InUse}) ->
+ NewInUse = if InUse == 0 -> 0;
+ true -> InUse - Magnitude
+ end,
+ {noreply, maybe_notify(State, State#lim{in_use = NewInUse})}.
handle_info({'DOWN', _MonitorRef, _Type, QPid, _Info},
State = #lim{queues = Queues}) ->
@@ -117,6 +112,12 @@ code_change(_, State, _) ->
%% Internal plumbing
%%----------------------------------------------------------------------------
+maybe_notify(OldState, NewState) ->
+ case limit_reached(OldState) and not(limit_reached(NewState)) of
+ true -> forget_queues(NewState);
+ false -> NewState
+ end.
+
remember_queue(QPid, State = #lim{queues = Queues}) ->
case dict:is_key(QPid, Queues) of
false -> MonitorRef = erlang:monitor(process, QPid),
@@ -131,11 +132,6 @@ forget_queues(State = #lim{ch_pid = ChPid, queues = Queues}) ->
end, ok, Queues),
State#lim{queues = dict:new()}.
-decrement_in_use(_, State = #lim{in_use = 0}) ->
- State#lim{in_use = 0};
-decrement_in_use(Magnitude, State = #lim{in_use = InUse}) ->
- State#lim{in_use = InUse - Magnitude}.
-
limit_reached(#lim{prefetch_count = 0}) ->
false;
limit_reached(#lim{prefetch_count = Limit, in_use = InUse}) ->