diff --git a/lib/amqp/channel.ex b/lib/amqp/channel.ex index ddfb10c..607f314 100644 --- a/lib/amqp/channel.ex +++ b/lib/amqp/channel.ex @@ -21,7 +21,7 @@ defmodule Amqpx.Channel do @spec open(Connection.t(), pid()) :: {:ok, Channel.t()} | {:error, any} def open(%Connection{pid: pid} = conn, consumer_pid) do - case :amqp_connection.open_channel(pid, {:amqp_direct_consumer, [consumer_pid]}) do + case :amqp_connection.open_channel(pid, {Amqpx.DirectConsumer, [consumer_pid]}) do {:ok, chan_pid} -> {:ok, %Channel{conn: conn, pid: chan_pid}} error -> error end diff --git a/lib/amqp/direct_consumer.ex b/lib/amqp/direct_consumer.ex new file mode 100644 index 0000000..ba05ff0 --- /dev/null +++ b/lib/amqp/direct_consumer.ex @@ -0,0 +1,46 @@ +defmodule Amqpx.DirectConsumer do + @moduledoc """ + This module "wraps" the [amqp_direct_consumer](https://github.com/rabbitmq/rabbitmq-server/blob/main/deps/amqp_client/src/amqp_direct_consumer.erl) module to manage the race condition that occurs at supervisor shutdown, leading to error logs such as: + ``` + [error] GenServer #PID<0.846.0> terminating + ** (stop) {:error, {:consumer_died, :shutdown}} + Last message: {:DOWN, #Reference<0.1010163881.3799777283.106124>, :process, #PID<0.798.0>, :shutdown}. + ``` + The reason for this behavior is that the `amqp_direct_consumer` module considers any exit reason other than `:normal` as an error, prompting it to exit. + In our implementation, we have introduced two pattern matches to treat the exit reasons `:shutdown` and `{:shutdown, reason}` as normal occurrences; these conditions typically arise when the supervisor terminates the consumer, such as during a standard shutdown of the application." + """ + + @behaviour :amqp_gen_consumer + + defdelegate init(opts), to: :amqp_direct_consumer + + defdelegate handle_consume(m, a, c), to: :amqp_direct_consumer + + defdelegate handle_consume_ok(m, a, c), to: :amqp_direct_consumer + + defdelegate handle_cancel(m, c), to: :amqp_direct_consumer + + defdelegate handle_cancel_ok(m, a, c), to: :amqp_direct_consumer + + defdelegate handle_server_cancel(m, c), to: :amqp_direct_consumer + + defdelegate handle_deliver(m, a, c), to: :amqp_direct_consumer + + defdelegate handle_deliver(m, a, delivery_ctx, c), to: :amqp_direct_consumer + + defdelegate handle_call(m, a, c), to: :amqp_direct_consumer + + defdelegate terminate(reason, c), to: :amqp_direct_consumer + + def handle_info({:DOWN, _mref, :process, c, :shutdown}, c) do + {:ok, c} + end + + def handle_info({:DOWN, _mref, :process, c, {:shutdown, _}}, c) do + {:ok, c} + end + + def handle_info(m, c) do + :amqp_direct_consumer.handle_info(m, c) + end +end