-
-
Notifications
You must be signed in to change notification settings - Fork 2
MultiplexRpcTransporter strict timeout . #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,16 +26,22 @@ public function receive() | |||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| stream_set_blocking($this->client, false); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| $remainingTimeout = $this->timeout; | ||||||||||||||||||||||||||||||
| $start = microtime(true); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| while (true) { | ||||||||||||||||||||||||||||||
| $header = $this->readBytes(4); | ||||||||||||||||||||||||||||||
| $header = $this->readBytes(4, $remainingTimeout); | ||||||||||||||||||||||||||||||
| $remainingTimeout = max(0, $remainingTimeout - (microtime(true) - $start)); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| $unpacked = unpack('Nlength', $header); | ||||||||||||||||||||||||||||||
| $length = $unpacked['length']; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if ($length < 4) { | ||||||||||||||||||||||||||||||
| throw new RecvFailedException(sprintf('Invalid package length: %d', $length)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| $body = $this->readBytes($length); | ||||||||||||||||||||||||||||||
| $body = $this->readBytes($length, $remainingTimeout); | ||||||||||||||||||||||||||||||
| $remainingTimeout = max(0, $remainingTimeout - (microtime(true) - $start)); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (in_array($body, [self::PING, self::PONG], true)) { | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
@@ -47,7 +53,7 @@ public function receive() | |||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * @throws Exception | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| private function readBytes(int $length): string | ||||||||||||||||||||||||||||||
| private function readBytes(int $length, float $timeout): string | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| $buffer = ''; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -56,7 +62,10 @@ private function readBytes(int $length): string | |||||||||||||||||||||||||||||
| $write = null; | ||||||||||||||||||||||||||||||
| $except = null; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| $selected = stream_select($read, $write, $except, $this->timeout); | ||||||||||||||||||||||||||||||
| $timeoutSeconds = (int) $timeout; | ||||||||||||||||||||||||||||||
| $timeoutMicroseconds = (int) (($timeout - $timeoutSeconds) * 1000000); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| $selected = stream_select($read, $write, $except, $timeoutSeconds, $timeoutMicroseconds); | ||||||||||||||||||||||||||||||
|
Comment on lines
+65
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Preserve the deadline across partial reads.
Proposed fix private function readBytes(int $length, float $timeout): string
{
$buffer = '';
+ $deadline = microtime(true) + $timeout;
while (strlen($buffer) < $length) {
$read = [$this->client];
$write = null;
$except = null;
- $timeoutSeconds = (int) $timeout;
- $timeoutMicroseconds = (int) (($timeout - $timeoutSeconds) * 1000000);
+ $remaining = $deadline - microtime(true);
+ if ($remaining <= 0) {
+ throw new RecvFailedException('Receive timeout.');
+ }
+ $timeoutSeconds = (int) $remaining;
+ $timeoutMicroseconds = (int) (($remaining - $timeoutSeconds) * 1000000);
$selected = stream_select($read, $write, $except, $timeoutSeconds, $timeoutMicroseconds);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| if ($selected === false) { | ||||||||||||||||||||||||||||||
| throw new RuntimeException('Failed to select stream.'); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: friendsofhyperf/jet
Length of output: 255
🏁 Script executed:
Repository: friendsofhyperf/jet
Length of output: 1566
🏁 Script executed:
Repository: friendsofhyperf/jet
Length of output: 6493
Validate
data['class']before using it in the exception message.??only provides a default for missing keys; it does not prevent offset access on non-arraydata. Also, non-stringclassvalues still enter the concatenation and can fire conversion warnings/errors duringServerExceptionconstruction. Extractdatafirst, check it is an array, and only use non-empty stringclassvalues.🤖 Prompt for AI Agents