-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[STAFF-31] Add proper shutdown handling to Amqpx generic producers and consumers #138
[STAFF-31] Add proper shutdown handling to Amqpx generic producers and consumers #138
Conversation
which turns off the channel normally when reason is `:normal`, `:shutdown`, or `{:shutdown, term}`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ✨
case reason do | ||
:normal -> close_channel(channel) | ||
:shutdown -> close_channel(channel) | ||
{:shutdown, _} -> close_channel(channel) | ||
_ -> :ok |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we close the channel regardless of the reason? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the channel is always closed because it is linked. However, for "normal" closures, we need to add some logic, since as you can see, amqp_client does not consider {:shutdown, term}
or simply :shutdown
as normal closures
@@ -26,10 +26,11 @@ defmodule Amqpx.Gen.Consumer do | |||
|
|||
@gen_server_opts [:name, :timeout, :debug, :spawn_opt, :hibernate_after] | |||
|
|||
@spec start_link(opts :: map()) :: GenServer.server() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops... Maybe here a spec can help :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we only give a default name to the producer?
I've only replicated the previous behavior, but I suppose it's because a consumer usually interacts with others, and it's rare for others to ask things of them, while a producer is typically the one being contacted |
Previously, the Amqpx producers and consumers did not handle shutdown gracefully, resulting in noisy log messages and a strange behavior when the supervisor attempted to shut them down.
To address this issue and to support use cases where the actors need to be shut down gracefully, I have added a new
terminate
function to the actors implementation. It gracefully shuts down the associated channels when reasons like:normal
,:shutdown
, and{:shutdown, reason :: term()}
are encountered.This change also enables the use of the library in distributed environments, where the actors may need to be shut down deliberately, such as when balancing nodes in a cluster.