summaryrefslogtreecommitdiff
path: root/src
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
commite83062c4a946ab1de13cfb6459d07595eb122ed3 (patch)
tree52b816281c6bec69190b8bf6635c543a8ee2b54c /src
parent2c24c963e71d7f0070aa368ab97efbcfb329706f (diff)
downloadrabbitmq-server-git-e83062c4a946ab1de13cfb6459d07595eb122ed3.tar.gz
refactoring
Diffstat (limited to 'src')
-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 cd8f773436..6388c3601f 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}) ->