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

Throw connection exceptions on batch publish #983

Merged
merged 3 commits into from
Mar 31, 2022

Conversation

foment
Copy link
Contributor

@foment foment commented Mar 10, 2022

Hello,

in bulk publishing, the connection existence is not checked (PhpAmqpLib/Channel/AMQPChannel.php:1232 in publish_batch method) and therefore we can't catch proper exception, like for example AMQPChannelClosedException.

This sometimes happens when there is a delay between opening the connection and actual publishing and for example in the meantime connection was closed for any reason whatsoever.

Therefore, here is the suggestion to throw a proper exception already in the prepare_method_frame method, like it's done for send_method_frame method in the AbstractChannel class.

@ramunasd ramunasd self-assigned this Mar 11, 2022
@foment
Copy link
Contributor Author

foment commented Mar 29, 2022

Hi,
any update on this PR?

Cheers :)

@ramunasd
Copy link
Member

Hi @foment , thanks for pointing this out and your efforts. Can You explain why You picked method prepare_method_frame ? This one is called only from prePublish() and only when "publish cache" is not warmed up.
Isn't better to check connection in publish_batch() ? Well, there is one check, maybe it's better to move it to beginning of method?
Anyways, this check does not garantuee stable connection. I would like to suggest enabling keepalive and heartbeat with signal senders. They are doing great job when scripts aren't responsive enough.

@foment
Copy link
Contributor Author

foment commented Mar 30, 2022

Hi @ramunasd,

thank you for the quick reply.

The idea behind throwing an exception in prepare_method_frame is more to be aligned with the current implementation like in send_method_frame for example, where connection is checked for it's existence. You're right, as an alternative, I could move $this->checkConnection(); call in the publish_batch method to the beginning (before actual call to prePublish method).

One of the reasons behind all of this is to be able to catch proper exception in one of the legacy projects I'm currently working on and in general to have proper connection check before actually using it (like in publish_batch), which leads to php fatal error.

@ramunasd
Copy link
Member

So I don't see reason why "packet" method should be aware about connection. Please follow my suggestion. Thanks

@foment foment force-pushed the exception-on-empty-connection branch from 5b04e51 to c1d9adb Compare March 30, 2022 10:21
@foment
Copy link
Contributor Author

foment commented Mar 30, 2022

PR updated with implemented suggestion.

@codecov
Copy link

codecov bot commented Mar 30, 2022

Codecov Report

Merging #983 (9684db1) into master (1718c20) will increase coverage by 0.85%.
The diff coverage is 100.00%.

@@             Coverage Diff              @@
##             master     #983      +/-   ##
============================================
+ Coverage     73.53%   74.39%   +0.85%     
  Complexity      982      982              
============================================
  Files            35       35              
  Lines          2675     2675              
============================================
+ Hits           1967     1990      +23     
+ Misses          708      685      -23     
Impacted Files Coverage Δ
PhpAmqpLib/Channel/AMQPChannel.php 57.98% <100.00%> (+5.39%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1718c20...9684db1. Read the comment docs.

@ramunasd
Copy link
Member

We still use older version of phpunit :)

@foment
Copy link
Contributor Author

foment commented Mar 30, 2022

Fix is on the way :)

@ramunasd ramunasd added the bug label Mar 31, 2022
@ramunasd ramunasd added this to the 3.2.1 milestone Mar 31, 2022
@ramunasd ramunasd changed the title Exception on empty connection Throw connection exceptions on batch publish Mar 31, 2022
@ramunasd ramunasd merged commit 2736c5d into php-amqplib:master Mar 31, 2022
kratkyzobak pushed a commit to kratkyzobak/php-amqplib that referenced this pull request Feb 9, 2024
* Move connection check to the top of publish_batch method

* Add unit tests for publish_batch method and opened/closed connection

* Add support for older PHPUnit versions

Co-authored-by: Aleksandar Jaksic <aleksandar.jaksic@check24.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants