Priority queue operations in rabbitmq

Source: Internet
Author: User

1.% common queue operations
In (x, 0, {queue, [_] = In, [], 1})-> {queue, [x], in, 2 };
In (x, 0, {queue, in, out, Len}) When is_list (in), is_list (out)-> {queue, [x | in], out, len + 1 };

Priority queue operations:
In (x, priority,Q = {queue, [], [], 0})-> in (x, priority, {pqueue, []});
In (x, priority, q = {queue,
,,})-> In (x, priority, {pqueue, [{0, Q}]});

Main Operations:

in(X, Priority, {pqueue, Queues}) ->P = maybe_negate_priority(Priority),{pqueue, case lists:keysearch(P, 1, Queues) of             {value, {_, Q}} ->                 lists:keyreplace(P, 1, Queues, {P, in(X, Q)});             false when P == infinity ->                 [{P, {queue, [X], [], 1}} | Queues];             false ->                 case Queues of                     [{infinity, InfQueue} | Queues1] ->                         [{infinity, InfQueue} |                          lists:keysort(1, [{P, {queue, [X], [], 1}} | Queues1])];                     _ ->                         lists:keysort(1, [{P, {queue, [X], [], 1}} | Queues])                 end         end}.

Sort by priority of the priority queue. If the priority of the queue is infinity, it is placed in the header. Other queues are sorted by priority.

2.% actions on the columns. If the columns are empty after the queue is out, the r2f (in, Len-1) is taken out of the queue to the queue.
Out ({queue, [], [], 0} = q)-> {empty, Q };
Out ({queue, [v], [], 1})-> {value, V}, {queue, [], [], 0 }};
Out ({queue, [Y | in], [], Len})->

[V|Out] = lists:reverse(In, []),{{value, V}, {queue, [Y], Out}, Len - 1};

Out ({queue, In, [v], Len}) When is_list (in)-> {value, V}, r2f (in, len-1 )};
Out ({queue, In, [v | out], Len}) When is_list (in)-> {value, V}, {queue, in, out, l-EN1 }};

Key steps for outbound queue:

out({pqueue, [{P, Q} | Queues]}) ->{R, Q1} = out(Q),NewQ = case is_empty(Q1) of           true -> case Queues of                       []           -> {queue, [], [], 0};                       [{0, OnlyQ}] -> OnlyQ;                       [_|_]        -> {pqueue, Queues}                   end;           false -> {pqueue, [{P, Q1} | Queues]}       end,{R, NewQ}.

For a queue with a priority, if the Q queue is empty after the priority is retrieved, determine the next queue queues. Otherwise, {pqueue, [{P, Q1} | queues]} is returned.

Conclusion: The number formats returned by the outbound queue operations are different, similar to {value, V}, {queue, in, out, l-EN1 }}

Priority queue operations in rabbitmq

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.