8000 [Messenger] Provides a safe single handler strategy · Issue #36958 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[Messenger] Provides a safe single handler strategy #36958
Closed
@gquemener

Description

@gquemener

Description
(This discussion is following #29167)

Single handler strategy is common when using a command bus (as a command should be handled by one and only one handler).
Currently, the only way to enforce this strategy is through the HandleTrait which performs the check after the message has been handled (which can have harmful consequences).

I propose to implement a safe (meaning that prevents any handler to be called) check.
Once this is implemented, I don't see any reason to keep the HandleTrait.

So far, I see a few ways to achieve that.

Add a allowMultipleHandlers flag to the HandleMessageMiddleware.

Then it would be a matter of

        $handlers = $this->handlersLocator->getHandlers($envelope);
        if (!$this->allowMultipleHandlers && count($handlers) > 1) {
            throw new TooManyHandlersForMessageException(sprintf('Too many handlers are registered for message "%s".', $context['class']));
        }
        foreach ($handlers as $handlerDescriptor) {

This flag should be true by default to be BC.

Add a new implementation of HandlersLocatorInterface

This implementation would throw an exception when more than one handler is located. The correct implementation would most likelly be injected through the MessengerPass or the FrameworkExtension.

Add the check during compilation

The check could even live in the MessengerPass or the FrameworkExtension itself. The drawback is that this would couple it with the framework and prevent single handler strategy to be used without the framework.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0