Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow prefetch_count when consuming a channel. #27

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/clients/rabbitmq.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ defmodule ExRabbitPool.RabbitMQ do
end

@impl true
def consume(%Channel{} = channel, queue, consumer_pid \\ nil, options \\ []) do
def consume(channel, queue, consumer_pid \\ nil, options \\ [])

def consume(%Channel{} = channel, queue, consumer_pid, [prefetch_count: prefetch_count] = options) do
Logger.warn("[ExRabbitPool.RabbitMQ.consume] queue: #{inspect queue} setting prefetch_count to #{prefetch_count}")
:ok = Basic.qos(channel, prefetch_count: prefetch_count)
Basic.consume(channel, queue, consumer_pid, options)
end

def consume(%Channel{} = channel, queue, consumer_pid, options) do
Basic.consume(channel, queue, consumer_pid, options)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ defmodule ExRabbitPool.Consumer do
# process and monitors it handle crashes and reconnections
defp handle_channel_checkout(
{:ok, %{pid: channel_pid} = channel},
%{config: config, queue: queue, adapter: adapter, config: config} = state
%{config: config, queue: queue, adapter: adapter} = state
) do
config = Keyword.get(config, :options, [])

Expand Down
23 changes: 14 additions & 9 deletions test/integration/consumer_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ExRabbitPool.ConsumerTest do
use ExUnit.Case, async: false

import ExUnit.CaptureLog
alias ExRabbitPool.Worker.SetupQueue
alias ExRabbitPool.RabbitMQ
alias AMQP.Queue
Expand Down Expand Up @@ -86,15 +87,19 @@ defmodule ExRabbitPool.ConsumerTest do
end

test "should be able to consume messages out of rabbitmq", %{pool_id: pool_id, queue: queue} do
pid = start_supervised!({TestConsumer, pool_id: pool_id, queue: queue})
:erlang.trace(pid, true, [:receive])

ExRabbitPool.with_channel(pool_id, fn {:ok, channel} ->
assert :ok = RabbitMQ.publish(channel, "#{queue}_exchange", "", "Hello Consumer!")
assert_receive {:trace, ^pid, :receive, {:basic_deliver, "Hello Consumer!", _}}, 1000
{:ok, result} = Queue.status(channel, queue)
assert result == %{consumer_count: 1, message_count: 0, queue: queue}
end)
logs =
capture_log(fn ->
pid = start_supervised!({TestConsumer, pool_id: pool_id, queue: queue, options: [prefetch_count: 19]})
:erlang.trace(pid, true, [:receive])

ExRabbitPool.with_channel(pool_id, fn {:ok, channel} ->
assert :ok = RabbitMQ.publish(channel, "#{queue}_exchange", "", "Hello Consumer!")
assert_receive {:trace, ^pid, :receive, {:basic_deliver, "Hello Consumer!", _}}, 1000
{:ok, result} = Queue.status(channel, queue)
assert result == %{consumer_count: 1, message_count: 0, queue: queue}
end)
end)
assert logs =~ "[ExRabbitPool.RabbitMQ.consume] queue: #{inspect queue} setting prefetch_count to 19"
end

test "should be able to consume messages out of rabbitmq with default consumer", %{
Expand Down