Skip to content

[Messenger] Remove StackInterface #31179

Closed
@volkyeth

Description

@volkyeth

Description
We should drop the Stack concept and reintroduce $next in MiddlewareInterface::handle, like in the good old days:

interface MiddlewareInterface
{
    /**
     * @param callable|NextInterface $next
     */
    public function handle(Envelope $envelope, callable $next): Envelope;
}

/**	
 * @internal	
 */	
interface NextInterface	
{	
    public function __invoke(Envelope $envelope): void;	
}

Update: New proposal on #31185

My case agains the Stack is:

  1. It makes middlewares harder to test

Instead of calling our Middlewares with a simple assertion function we must create a Stack.
When trying to use a simple Stack spy we have to mock both the next() and handle() methods

With the Stack:

$stackSpy = $this->prophesize(StackInterface::Class);
$stackSpy->next()->shouldBeCalledOnce()->willReturn($stackSpy);
$stackSpy->handle($envelope, $stackSpy)->shouldBeCalledOnce()->willReturn($envelope);

$myMiddleware->handle($envelope, $stackSpy->reveal());

Without the stack:

$nextSpy = $this->prophesize(NextInterface::Class);
$nextSpy->__invoke($envelope)->shouldBeCalledOnce()->willReturn($envelope);

$myMiddleware->handle($nextSpy->reveal());
  1. It introduces non-determinism in the bus

Because of the iterator that lives inside the stack we cannot use middlewares to control the bus flow without getting into weird behaviours.

For instance, if not for the stack, the Retry mechanism (#30557, #27215) could be implemented as a Middleware in a trivial way, with the added benefit of being able to retry from specific positions in the pipeline:

    public function handle(Envelope $envelope, callable $next) : Envelope
    {
        while ($this->tries-- > 0) {
            try {
                return $next($envelope);
            } catch (\Throwable $t) {
            }
        }
        
        throw $t;
    }

If we try this with the Stack it's internal iterator will have advanced, making the middlewares pipeline non-reproducible.

  1. It makes middlewares more verbose
$stack->next()->handle($envelope, $stack);

vs

$next($envelope);

Metadata

Metadata

Assignees

No one assigned

    Labels

    MessengerRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions