-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with chunked reading from process and extract it to separat…
…e class
- Loading branch information
1 parent
7f6b694
commit 2cae3fc
Showing
2 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Manticoresearch\Buddy\Core\Process; | ||
|
||
use Generator; | ||
use RuntimeException; | ||
use Swoole\Process as SwooleProcess; | ||
|
||
/** @package */ | ||
final class ProcessReader { | ||
/** | ||
* Serialize and pack message | ||
* @param array<mixed> $message | ||
* @return string | ||
*/ | ||
public static function packMessage(array $message): string { | ||
$serialized = serialize($message); | ||
return pack('N', strlen($serialized)) . $serialized; | ||
} | ||
|
||
/** | ||
* Unpack the message and returns message to unserialize and rest of it in case have we have incomplete | ||
* @param string $message | ||
* @return array{0:string,1:string} | ||
* @throws RuntimeException | ||
*/ | ||
public static function unpackMessage(string $message): array { | ||
$length = static::getMessageLen($message); | ||
$chunk = substr($message, 4, $length); | ||
return [$chunk, substr($message, $length + 4)]; | ||
} | ||
|
||
/** | ||
* Validate if the message is complete or not | ||
* cuz sometimes Swoole may send it in the different packages | ||
* @param string $message | ||
* @return bool | ||
*/ | ||
public static function isMessageComplete(string $message): bool { | ||
$length = static::getMessageLen($message); | ||
return strlen($message) >= $length + 4; | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @return int | ||
* @throws RuntimeException | ||
*/ | ||
protected static function getMessageLen(string $message): int { | ||
$unpacked = unpack('N', $message); | ||
if (!$unpacked) { | ||
throw new RuntimeException('Failed to unpack message'); | ||
} | ||
return $unpacked[1]; | ||
} | ||
|
||
/** | ||
* Wrapper to read chunked message from the worker | ||
* @param SwooleProcess $worker | ||
* @return Generator<string> | ||
*/ | ||
public static function read(SwooleProcess $worker): Generator { | ||
$buffer = ''; | ||
$chunk = $worker->read(); | ||
|
||
if ($chunk) { | ||
$buffer .= $chunk; | ||
|
||
while ($buffer) { | ||
if (!static::isMessageComplete($buffer)) { | ||
break; | ||
} | ||
|
||
[$current, $remaining] = static::unpackMessage($buffer); | ||
yield $current; | ||
|
||
$buffer = $remaining; | ||
} | ||
} | ||
|
||
return $buffer; | ||
} | ||
} |