Skip to content

Commit 0328837

Browse files
committed
Simplify consumer cancellation in QQ for single active consumer
References #1799
1 parent 71d17fb commit 0328837

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/rabbit_fifo.erl

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -727,33 +727,25 @@ cancel_consumer(ConsumerId,
727727
waiting_consumers = WaitingConsumers0 } = State0}) ->
728728
%% single active consumer on, consumers are waiting
729729
case maps:take(ConsumerId, Cons0) of
730-
{_CurrentActiveConsumer, _} ->
730+
{_CurrentActiveConsumer = #consumer{checked_out = Checked0}, _} ->
731731
% The active consumer is to be removed
732732
% Cancel it
733-
{Effects1, State1} = case maps:take(ConsumerId, Cons0) of
734-
{#consumer{checked_out = Checked0}, _} ->
735-
S = return_all(State0, Checked0),
736-
Effects = cancel_consumer_effects(ConsumerId, S, Effects0),
737-
{Effects, State0};
738-
error ->
739-
{Effects0, State0}
740-
end,
733+
S = return_all(State0, Checked0),
734+
Effects = cancel_consumer_effects(ConsumerId, S, Effects0),
741735
% Take another one from the waiting consumers and put it in consumers
742736
[{NewActiveConsumerId, NewActiveConsumer} | RemainingWaitingConsumers] = WaitingConsumers0,
743737
#state{service_queue = ServiceQueue} = State0,
744738
ServiceQueue1 = maybe_queue_consumer(NewActiveConsumerId, NewActiveConsumer, ServiceQueue),
745-
State2 = State1#state{consumers = #{NewActiveConsumerId => NewActiveConsumer},
739+
State1 = State0#state{consumers = #{NewActiveConsumerId => NewActiveConsumer},
746740
service_queue = ServiceQueue1,
747741
waiting_consumers = RemainingWaitingConsumers},
748-
{Effects1, State2};
742+
{Effects, State1};
749743
error ->
750744
% The cancelled consumer is not the active one
751745
% Just remove it from idle_consumers
752-
{value, {ConsumerId, #consumer{checked_out = Checked0}}, WaitingConsumers1} =
753-
lists:keytake(ConsumerId, 1, WaitingConsumers0),
754-
S = return_all(State0, Checked0),
755-
Effects = cancel_consumer_effects(ConsumerId, S, Effects0),
756-
{Effects, State0#state{waiting_consumers = WaitingConsumers1}}
746+
{value, _Consumer, WaitingConsumers1} = lists:keytake(ConsumerId, 1, WaitingConsumers0),
747+
% A waiting consumer isn't supposed to have any checked out messages, so nothing special to do here
748+
{Effects0, State0#state{waiting_consumers = WaitingConsumers1}}
757749
end.
758750

759751
cancel_consumer0(ConsumerId,

0 commit comments

Comments
 (0)