Skip to content

Conversation

tomas-novotny
Copy link
Contributor

Make possible to set custom LoopInterface instance into newly created Client.

@tomas-novotny tomas-novotny changed the title Optional loop instance to client [0.6.x] Optional loop instance to client Jun 12, 2025
@WyriHaximus WyriHaximus added this to the v0.6.0 milestone Jun 12, 2025
@WyriHaximus
Copy link
Collaborator

@tomas-novotny What's the use case here? There should be only one event loop, but if it's unit tests, we might as well make the connector configurable.

@WyriHaximus WyriHaximus self-requested a review June 12, 2025 21:38
@tomas-novotny
Copy link
Contributor Author

In some our application we have (mostly from historical reasons) greater control over Loop, which we are starting with Loop::run() and have it accessible in DI container. We create LoopInterface instance with Factory::create(), which does not trigger Loop auto-run with register_shutdown_function.

This helps mostly in unit-tests but even in some low-level libraries with optional Loop dependencies.

It works fine most of the time, but if dependencies are in wrong order two Loop instances are created. I know it is discouraged to use Factory::create(), but even after refactoring to use Loop:get() (or no Loop dependency at all) it still did not work perfectly.

Configurable connector is also a solution, but if this package does not want to communicate usage of Loop, it's just fine and i don't have problem with that.

@WyriHaximus
Copy link
Collaborator

Configurable connector is also a solution, but if this package does not want to communicate usage of Loop, it's just fine and i don't have problem with that.

So the ReactPHP project itself is moving the global Loop accessor, we've made the loop arguments for most if not all classes optional. And I'm following what we're doing there but skipping the optional step. So injecting through the Connector has the preference. However...:

In some our application we have (mostly from historical reasons) greater control over Loop, which we are starting with Loop::run() and have it accessible in DI container. We create LoopInterface instance with Factory::create(), which does not trigger Loop auto-run with register_shutdown_function.

This helps mostly in unit-tests but even in some low-level libraries with optional Loop dependencies.

It works fine most of the time, but if dependencies are in wrong order two Loop instances are created. I know it is discouraged to use Factory::create(), but even after refactoring to use Loop:get() (or no Loop dependency at all) it still did not work perfectly.

Oof that is rough, those are really shitty to track down. That does make this PR a bit more complicated. Because, aside for being side tracked and not responding ealier, sorry for that, I was going to ask if you wanted to make the changes or if you wanted me to do that. There are 4 places where the global loop accessor is used in the Connection class: https://github.com/search?q=repo%3Ajakubkulhan%2Fbunny+Loop%3A%3A+path%3A%2F%5Esrc%5C%2F%2F&type=code

Have you tested this change in your situation and does it solve the issues you're seeing?

Here are the two options we have IMHO:

  1. Go with the injectable Connector
  2. Make the loop an optional argument and do it all the way for 0.6 and mark it deprecated, and remove it in 0.7 to give you more time fix this issue on your end. I do want it gone by 1.0, but it's not a hard thing just yet.

@tomas-novotny
Copy link
Contributor Author

Like this or would you prefer to add it to Bunny\Configuration ?

@WyriHaximus
Copy link
Collaborator

Like this or would you prefer to add it to Bunny\Configuration ?

Going to add it to Bunny\Configuration as $connector, and do some reactorings there so I'm going to merge this PR as it. Let me know if this doesn't solve the issues you are having in your tests, and we'll go with option two.

Copy link
Collaborator

@WyriHaximus WyriHaximus left a comment

Choose a reason for hiding this comment

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

Thanks again as always for your great work!

@WyriHaximus WyriHaximus merged commit 208b4bf into jakubkulhan:0.6.x Jul 19, 2025
219 of 220 checks passed
@WyriHaximus
Copy link
Collaborator

@tomas-novotny Just merged #181 and #182, will take the first alpha some time next week. Let me know if you ran into issues with your tests.

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

Successfully merging this pull request may close these issues.

2 participants