Skip to content
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

Merged
merged 3 commits into from
Mar 17, 2023

Conversation

claudio-dalicandro
Copy link
Contributor

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.

which turns off the channel normally when reason is
`:normal`, `:shutdown`, or `{:shutdown, term}`
luca-tansini
luca-tansini previously approved these changes Mar 14, 2023
Copy link

@luca-tansini luca-tansini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✨

Comment on lines +184 to +188
case reason do
:normal -> close_channel(channel)
:shutdown -> close_channel(channel)
{:shutdown, _} -> close_channel(channel)
_ -> :ok
Copy link
Contributor

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? 🤔

Copy link
Contributor Author

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()
Copy link
Contributor Author

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

Copy link

@luca-tansini luca-tansini left a 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?

@claudio-dalicandro
Copy link
Contributor Author

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

@claudio-dalicandro claudio-dalicandro requested review from cottinisimone and removed request for cando March 17, 2023 12:19
@cottinisimone cottinisimone merged commit 19769b0 into master Mar 17, 2023
@cottinisimone cottinisimone deleted the STAFF-31/spike/graceful-shutdown-for-consumers branch March 17, 2023 13:35
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants