8000 [Messenger] make middlewares truly lazy on a bus by nicolas-grekas · Pull Request #28907 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] make middlewares truly lazy on a bus #28907

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

Merged
merged 1 commit into from
Oct 21, 2018

Conversation

nicolas-grekas
Copy link
Member
Q A
Branch? 4.2
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Right now, as soon as a message is dispatched on a bus, all its handlers are instantiated.
This PR fixes it by leveraging IteratorAggregate when possible (implemented by DI's RewindableGenerator).

@nicolas-grekas nicolas-grekas force-pushed the messenger-lazy branch 3 times, most recently from fe04974 to 3e71a94 Compare October 17, 2018 18:12
} elseif (\is_array($middlewareHandlers)) {
$this->middlewareAggregate = new \ArrayObject($middlewareHandlers);
} else {
$this->middlewareAggregate = new \ArrayObject(iterator_to_array($middlewareHandlers, false));
Copy link
Contributor

Choose a reason for hiding this comment

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

previously the expansion (iterator_to_array) happened lazily, should we preserve it?

return $this->aggregate = new \ArrayObject(iterator_to_array($this->iterator, false));
}
};
$this->middlewareAggregate->aggregate = &$this->middlewareAggregate;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move that to be in the constructor of your class instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

The class is internal, the public properties are not accessible. Adding a constructor would just add unneeded boilerplate. Better not to me.

if (null === $this->indexedMiddlewareHandlers) {
$this->indexedMiddlewareHandlers = \is_array($this->middlewareHandlers) ? array_values($this->middlewareHandlers) : iterator_to_array($this->middlewareHandlers, false);
$middlewareIterator = $this->middlewareAggregate->getIterator();
while ($middlewareIterator instanceof \IteratorAggregate) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why while? What's the use-case in which it will return an iterator twice (or more)?

Copy link
Contributor

Choose a reason for hiding this comment

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

@sroze i suggested that, see #28907 (comment)

Copy link
Contributor
@ro0NL ro0NL Oct 21, 2018

Choose a reason for hiding this comment

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

but maybe we should enforce it resolves once, either here or in the constructor 🤔

Copy link
Member Author
@nicolas-grekas nicolas-grekas Oct 21, 2018

Choose a reason for hiding this comment

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

That's how "foreach" works internally. We must have it to preserve basic PHP semantics of Traversable.

@nicolas-grekas
Copy link
Member Author
nicolas-grekas commented Oct 21, 2018

PR rebased, ready.

@sroze
Copy link
Contributor
sroze commented Oct 21, 2018

Thank you @nicolas-grekas.

@sroze sroze merged commit 6a5d7a1 into symfony:master Oct 21, 2018
sroze added a commit that referenced this pull request Oct 21, 2018
…s-grekas)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Messenger] make middlewares truly lazy on a bus

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Right now, as soon as a message is dispatched on a bus, all its handlers are instantiated.
This PR fixes it by leveraging `IteratorAggregate` when possible (implemented by DI's `RewindableGenerator`).

Commits
-------

6a5d7a1 [Messenger] make middlewares truly lazy on a bus
@nicolas-grekas nicolas-grekas deleted the messenger-lazy branch October 23, 2018 12:56
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.

5 participants
0