-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Fix sequence counting #34
base: 4.x
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Spiral\Goridge; | ||
use Swoole\Coroutine as Co; | ||
use Swoole\Coroutine\Barrier; | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
/** | ||
* This example demonstrates how to use the package within Swoole coroutines. | ||
*/ | ||
Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]); | ||
Co\Run(function () { | ||
$barrier = Barrier::make(); | ||
for ($i = 0; $i < 3; $i++) { | ||
go(function () use ($barrier) { | ||
$rpc = new Goridge\RPC\RPC( | ||
Goridge\Relay::create('tcp://127.0.0.1:6001') | ||
); | ||
echo $rpc->call('App.Hi', 'Antony'); | ||
}); | ||
} | ||
Barrier::wait($barrier); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,9 +150,10 @@ public function call(string $method, mixed $payload, mixed $options = null): mix | |
$relay->send($this->packFrame($method, $payload)); | ||
|
||
// wait for the frame confirmation | ||
$frame = $this->getResponseFromRelay($relay, self::$seq, true); | ||
$frame = $this->getResponseFromRelay($relay, $this->sequence, true); | ||
|
||
self::$seq++; | ||
$this->sequence++; | ||
Comment on lines
+153
to
+156
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. Initialize The Apply this diff to initialize public function __construct(
array $relays,
int $asyncBufferThreshold = self::DEFAULT_BUFFER_THRESHOLD,
CodecInterface $codec = new JsonCodec()
) {
+ $this->sequence = 0;
// existing code...
}
|
||
|
||
return $this->decodeResponse($frame, $relay, $options); | ||
} | ||
|
@@ -164,8 +165,9 @@ public function callIgnoreResponse(string $method, mixed $payload): void | |
|
||
$relay->send($this->packFrame($method, $payload)); | ||
|
||
$seq = self::$seq; | ||
$seq = $this->sequence; | ||
self::$seq++; | ||
$this->sequence++; | ||
self::$occupiedRelays[$seq] = $relay; | ||
// Last index so no need for array_pop or stuff | ||
unset(self::$freeRelays[$relayIndex]); | ||
|
@@ -186,8 +188,9 @@ public function callAsync(string $method, mixed $payload): int | |
|
||
$relay->send($this->packFrame($method, $payload)); | ||
|
||
$seq = self::$seq; | ||
$seq = $this->sequence; | ||
self::$seq++; | ||
$this->sequence++; | ||
self::$occupiedRelays[$seq] = $relay; | ||
self::$seqToRelayMap[$seq] = $relay; | ||
// Last index so no need for array_pop or stuff | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,7 +15,7 @@ class RPC extends AbstractRPC | |||||||||||||||||||||||
|
||||||||||||||||||||||||
public function __construct( | ||||||||||||||||||||||||
private readonly RelayInterface $relay, | ||||||||||||||||||||||||
CodecInterface $codec = new JsonCodec(), | ||||||||||||||||||||||||
CodecInterface $codec = new JsonCodec(), | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
parent::__construct($codec); | ||||||||||||||||||||||||
|
@@ -32,11 +32,12 @@ public function call(string $method, mixed $payload, mixed $options = null): mix | |||||||||||||||||||||||
throw new RPCException('Invalid RPC frame, options missing'); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ($frame->options[0] !== self::$seq) { | ||||||||||||||||||||||||
if ($frame->options[0] !== $this->sequence) { | ||||||||||||||||||||||||
throw new RPCException('Invalid RPC frame, sequence mismatch'); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
self::$seq++; | ||||||||||||||||||||||||
$this->sequence++; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Comment on lines
+35
to
41
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. Approve instance-based sequence management, but address inconsistency. The changes from However, there's an inconsistency: The line Apply this diff to resolve the issue: if ($frame->options[0] !== $this->sequence) {
throw new RPCException('Invalid RPC frame, sequence mismatch');
}
-self::$seq++;
$this->sequence++; 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||
return $this->decodeResponse($frame, $this->relay, $options); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
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.
🛠️ Refactor suggestion
Consider optimizing RPC usage and improving configuration management.
Creating a new RPC instance for each coroutine might not be the most efficient approach. Consider reusing a single RPC instance across coroutines if possible.
The hardcoded IP and port (
tcp://127.0.0.1:6001
) could be moved to a configuration variable for better maintainability and flexibility.The direct
echo
of the RPC call result doesn't allow for proper error handling or output formatting. Consider wrapping this in a try-catch block and formatting the output.Here's a suggested refactor:
This refactored version improves configuration management, reuses the RPC instance, and adds basic error handling and output formatting.