Skip to content

Commit

Permalink
fix(io): fix readAll() blocks (#270)
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Nov 10, 2021
1 parent 243dc1c commit 3f2bde0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
6 changes: 6 additions & 0 deletions examples/io/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
use function microtime;
use function round;
use function strlen;
use const PHP_OS_FAMILY;

require __DIR__ . '/../../vendor/autoload.php';

Async\main(static function (): int {
if (PHP_OS_FAMILY === 'Windows') {
IO\output_handle()->writeAll('This example requires does not support Windows.');

return 0;
}

$args = getopt('i:o:t:');
$input_file = $args['i'] ?? '/dev/zero';
Expand Down
9 changes: 6 additions & 3 deletions examples/shell/concurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
$start = time();

Async\concurrent([
static fn() => Shell\execute('sleep', ['2']),
static fn() => Shell\execute('sleep', ['2']),
static fn() => Shell\execute('sleep', ['1']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
]);

$duration = time() - $start;
Expand Down
7 changes: 7 additions & 0 deletions examples/unix/concurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
use Psl\IO;
use Psl\Str;
use Psl\Unix;
use const PHP_OS_FAMILY;

require __DIR__ . '/../../vendor/autoload.php';

Async\main(static function (): int {
if (PHP_OS_FAMILY === 'Windows') {
IO\output_handle()->writeAll('This example requires does not support Windows.');

return 0;
}

$file = Filesystem\create_temporary_file(prefix: 'psl-examples') . ".sock";

$output = IO\output_handle();
Expand Down
8 changes: 5 additions & 3 deletions src/Psl/IO/ReadHandleConvenienceMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ trait ReadHandleConvenienceMethodsTrait
*/
public function readAll(?int $max_bytes = null, ?float $timeout = null): string
{
$to_read = $max_bytes ?? Internal\ResourceHandle::DEFAULT_READ_BUFFER_SIZE;
$to_read = $max_bytes;

/** @var Psl\Ref<string> $data */
$data = new Psl\Ref('');
Expand All @@ -51,8 +51,10 @@ static function () use ($data): void {
/** @psalm-suppress MissingThrowsDocblock */
$chunk = $this->read($chunk_size, $timer->getRemaining());
$data->value .= $chunk;
$to_read -= strlen($chunk);
} while ($to_read > 0 && $chunk !== '');
if ($to_read !== null) {
$to_read -= strlen($chunk);
}
} while (($to_read === null || $to_read > 0) && $chunk !== '');

return $data->value;
}
Expand Down

0 comments on commit 3f2bde0

Please sign in to comment.