Skip to content

[Refactoring] Make the ReceiverInterface::receive(): ?Envelope non-null mandatory #8

Description

@YanGusik

Make the ReceiverInterface ->receive() Envelope non-null mandatory

interface ReceiverInterface
{
    /**
     * Blocks until a message is available.
     * Returns null when the worker should stop.
     */
    public function receive(): ?Envelope;

Even before threadpool, in the old implementation

I used:

public function receive(): ?Envelope
    {
        try {
            return $this->channel->recv();
        } catch (\Async\ChannelException) {
            return null;
        }
    }

and

 // Inner watcher
        $this->backgroundScope->spawn(function (): void {
            while (true) {
                try {
                    $envelope = $this->inner->receive();
                    $this->internalChannel->send(['inner', $envelope]);
                    if ($envelope === null) {
                        break;
                    }
                } catch (OperationCanceledException) {
                    break;
                } catch (\Throwable) {
                    try {
                        $this->internalChannel->send(['inner', null]);
                    } catch (\Throwable) {}
                    break;
                }
            }
        });

This is more of an architecture issue. On the one hand, it's very convenient to silently shut down the producer when receiving a null value.
On the other hand, the logic is very non-obvious and it's better to implement a try catch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions