8000 Add heartbeat tune config option #1216 by SunMar · Pull Request #1215 · php-amqplib/php-amqplib · GitHub
[go: up one dir, main page]

Skip to content

Add heartbeat tune config option #1216 #1215

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SunMar
Copy link
@SunMar SunMar commented May 2, 2025

Add a connection configuration option to determine connection heartbeat tuning, implements #1216.

@lukebakken lukebakken self-requested a review May 2, 2025 15:49
@lukebakken lukebakken self-assigned this May 2, 2025
Copy link
Collaborator
@lukebakken lukebakken left a comment

Choose a reason for hiding this comment

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

Hello, thanks for your interest in this.

This behavior is quite a bit different than the other AMQP 0.9.1 clients used with RabbitMQ. Would you please review how the Java client negotiates the heartbeat value and use a similar method here?

https://github.com/rabbitmq/rabbitmq-java-client/blob/main/src/main/java/com/rabbitmq/client/impl/AMQConnection.java#L425-L427

@SunMar
Copy link
Author
SunMar commented May 4, 2025

After checking the code, it appears the Java client does the following variation of lowest:

If both are greater than 0, use the lowest value of the two.
If client is 0, then client is ignored and server value is used.
If server is 0, then server is ignored and client value is used.
If both are 0, only then disable heartbeats.

That means the only difference between the Java client and php-amqplib (main branch, not this PR), is when client heartbeat is 0. Then in php-amqplib the heartbeat will be disabled, and in the Java client the server value is used.

If you want I can add that negotiation logic also as an option.

I can also limit the options and just go for these three:

  • PHP: use lowest, but disable if client is 0 (current php-amqplib behavior)
  • JAVA: use lowest, but only disable if both client and server are 0 (Java client behavior)
  • CLIENT: always use client value

Let me know what you prefer.

@lukebakken
Copy link
Collaborator

Please use the same logic as rabbitmq-java-client. Thank you for your contribution!

@SunMar
Copy link
Author
SunMar commented May 5, 2025

@lukebakken The point of the PR is to allow the client to choose a behavior. Simply replacing the logic would also introduce a breaking change.

If you do not want to add a feature to choose tuning behavior that's fine, then I will close the PR.

@lukebakken
Copy link
Collaborator

For the next major version, this client should be brought in-line with the Java client. Until then, the current behavior is fine. Thanks for looking into it.

@lukebakken lukebakken closed this May 5, 2025
@ramunasd
Copy link
Member
ramunasd commented May 5, 2025
8000

I personally liked this idea. We could start from 2-3 possible options, like default(backwards compatible), prefer broker, prefer client, auto.
Let's plan this for next major release.

@lukebakken
Copy link
Collaborator

Well that's a bummer, I can't unlock #1214. I can assign it to the 4.0 release.

@lukebakken lukebakken reopened this May 5, 2025
@lukebakken lukebakken added this to the 4.0.0 milestone May 5, 2025
@SunMar SunMar force-pushed the add-heartbeat-tune-option branch from 2bb23d6 to 5b5e9a3 Compare May 6, 2025 07:15
@SunMar
Copy link
Author
SunMar commented May 6, 2025

@ramunasd @lukebakken Ok, I've updated the PR, now there are 4 options:

  • LEGACY: current behavior
  • AUTO: Java client behavior
  • PREFER_BROKER: uses the broker value
  • PREFER_CLIENT: uses the client value

The default is set to LEGACY so that there is no breaking change. Maybe this is something that can be added in a minor release as there is no breaking change? Then maybe for the 4.0.0 release the change can be switching the default from LEGACY to AUTO (and possibly removing LEGACY, I've marked that as @deprecated now).

Let me know what you think.

@SunMar SunMar force-pushed the add-heartbeat-tune-option branch 2 times, most recently from 43117ac to dcc5202 Compare May 6, 2025 08:33
@SunMar SunMar changed the title Add heartbeat tune config option #1214 Add heartbeat tune config option #1216 May 6, 2025
@SunMar SunMar force-pushed the add-heartbeat-tune-option branch 2 times, most recently from 80dd91e to b5294ed Compare May 6, 2025 09:48
@@ -939,15 +939,32 @@ protected function connection_tune($args)
$this->frame_max = (int)$v;
}

// @see https://www.rabbitmq.com/heartbeats.html
Copy link
Member

Choose a reason for hiding this comment

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

please keep this comment

Copy link
Author

Choose a reason for hiding this comment

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

I added comments in AMQPConnectionConfig on each constant that explain how it works, so this comment felt like it became redundant. if you prefer to keep it shall I then move it to inside the case AMQPConnectionConfig::HEARTBEAT_TUNE_LEGACY: ? and do you then also want a comment in the other case statements?

Copy link
Author

Choose a reason for hiding this comment

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

also note that the comment contradictory:

If either value is 0 (see below), the greater value of the two is used

that is the Java client behavior, but the last line states:

For BC, this library opts for disabled heartbeat if client value is 0.

which is the actual implementation, so it only uses the greater value of the two if the broker value is 0

Copy link
Author

Choose a reason for hiding this comment

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

@ramunasd how would you like me to proceed here?

@SunMar SunMar force-pushed the add-heartbeat-tune-option branch from b5294ed to 9403b26 Compare May 6, 2025 18:32
@SunMar SunMar force-pushed the add-heartbeat-tune-option branch from 9403b26 to 59cabb4 Compare May 6, 2025 18:37
@SunMar
Copy link
Author
SunMar commented May 9, 2025

Interesting to note here too maybe, we did some checking on other client libraries we use.

Node's amqplib does the same negotiation as the Java client (use the lowest value between client and broker, unless one of them is 0, then take the max, only disable if both are 0).
https://github.com/amqp-node/amqplib/blob/28f1fd02170cc981dd69666dcc9f901c47a54ef2/lib/connection.js#L178-L179
https://github.com/amqp-node/amqplib/blob/28f1fd02170cc981dd69666dcc9f901c47a54ef2/lib/connection.js#L130-L143

Python's aio-pika always uses the client value and does not do negotiation. The property self.url is the client configuration in which a heartbeat can be specified through a query parameter (e.g. http://x.x.x.x/?heartbeat=60). Then after getting the connection tune the heartbeat value is simply overwritten with the client value.
https://github.com/mosquito/aiormq/blob/120965c68a18db7dcab6da2b7c66d4b2a86dea45/aiormq/connection.py#L312-L314
https://github.com/mosquito/aiormq/blob/120965c68a18db7dcab6da2b7c66d4b2a86dea45/aiormq/connection.py#L500

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0