Channel::isFull

(PHP 8.6+, True Async 1.0)

public Channel::isFull(): bool

Checks whether the channel buffer is filled to its maximum capacity.

For a rendezvous channel (capacity = 0), this always returns true, since there is no buffer.

Return values

true — the buffer is full (or it is a rendezvous channel). false — the buffer has free space.

Examples

Example #1 Checking buffer fullness

<?php

use Async\Channel;

$channel = new Channel(2);

echo $channel->isFull() ? "full" : "has space"; // "has space"

$channel->send('a');
$channel->send('b');

echo $channel->isFull() ? "full" : "has space"; // "full"

Example #2 Adaptive send rate

<?php

use Async\Channel;

$channel = new Channel(50);

spawn(function() use ($channel) {
    foreach (readLargeFile('data.csv') as $line) {
        if ($channel->isFull()) {
            echo "Buffer full, slowing down processing\n";
        }
        $channel->send($line); // suspends if full
    }
    $channel->close();
});

See also