Skip to content
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 #31

Merged
merged 6 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/swoole.php
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);
});
28 changes: 24 additions & 4 deletions src/RPC/RPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ class RPC implements RPCInterface

/**
* @var positive-int
* @deprecated since v3.2.1.
*/
private static int $seq = 1;
msmakouz marked this conversation as resolved.
Show resolved Hide resolved

/**
* @deprecated since v3.2.1. Need for backward compatibility.
*/
private bool $hasSequence = false;

/**
* @param RelayInterface $relay
* @param CodecInterface|null $codec
Expand All @@ -43,6 +49,8 @@ public function __construct(RelayInterface $relay, CodecInterface $codec = null)
{
$this->relay = $relay;
$this->codec = $codec ?? new JsonCodec();
/** @psalm-suppress DeprecatedProperty */
$this->hasSequence = \method_exists($this->relay, 'getNextSequence');
msmakouz marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -76,7 +84,10 @@ public function withCodec(CodecInterface $codec): RPCInterface
*/
public function call(string $method, $payload, $options = null)
{
$this->relay->send($this->packFrame($method, $payload));
/** @psalm-suppress DeprecatedMethod */
$seq = $this->getNextSequence();

$this->relay->send($this->packFrame($method, $payload, $seq));

// wait for the frame confirmation
$frame = $this->relay->waitFrame();
Expand All @@ -85,7 +96,7 @@ public function call(string $method, $payload, $options = null)
throw new RPCException('Invalid RPC frame, options missing');
}

if ($frame->options[0] !== self::$seq) {
if ($frame->options[0] !== $seq) {
throw new RPCException('Invalid RPC frame, sequence mismatch');
}

Expand Down Expand Up @@ -163,13 +174,22 @@ private function decodeResponse(Frame $frame, $options = null)
* @param mixed $payload
* @return Frame
*/
private function packFrame(string $method, $payload): Frame
private function packFrame(string $method, $payload, int $seq): Frame
{
if ($this->service !== null) {
$method = $this->service . '.' . \ucfirst($method);
}

$body = $method . $this->codec->encode($payload);
return new Frame($body, [self::$seq, \strlen($method)], $this->codec->getIndex());
return new Frame($body, [$seq, \strlen($method)], $this->codec->getIndex());
}

/**
* @deprecated since v3.2.1.
*/
private function getNextSequence(): int
{
/** @psalm-suppress DeprecatedProperty */
return $this->hasSequence ? $this->relay->getNextSequence() : self::$seq;
msmakouz marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 7 additions & 0 deletions src/Relay.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ abstract class Relay implements RelayInterface
public const PIPES = 'pipes';
protected const CONNECTION_EXP = '/(?P<protocol>[^:\/]+):\/\/(?P<arg1>[^:]+)(:(?P<arg2>[^:]+))?/';

private int $sequence = 1;

/**
* Create relay using string address.
*
Expand Down Expand Up @@ -93,4 +95,9 @@ private static function openOut(string $output)

return $resource;
}

public function getNextSequence(): int
{
return $this->sequence++;
}
}
5 changes: 1 addition & 4 deletions src/RelayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@

/**
* Blocking, duplex relay.
* @method getNextSequence(): int
*/
interface RelayInterface
{
/**
* @return Frame
* @throws RelayException
*/
public function waitFrame(): Frame;

/**
* @param Frame $frame
*/
public function send(Frame $frame): void;
}
52 changes: 51 additions & 1 deletion tests/Goridge/RPCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Exception;
use PHPUnit\Framework\TestCase;
use Spiral\Goridge\Frame;
use Spiral\Goridge\Relay;
use Spiral\Goridge\RelayInterface;
use Spiral\Goridge\RPC\Codec\RawCodec;
use Spiral\Goridge\RPC\Exception\CodecException;
Expand Down Expand Up @@ -155,7 +157,7 @@ public function testLongRawBody(): void
{
$conn = $this->makeRPC();
$payload = random_bytes(65000 * 1000);

$resp = $conn->withCodec(new RawCodec())->call(
'Service.EchoBinary',
$payload
Expand Down Expand Up @@ -248,6 +250,54 @@ public function testJsonException(): void
$conn->call('Service.Process', random_bytes(256));
}

/**
* @doesNotPerformAssertions
*/
public function testCallSequence(): void
{
$relay1 = $this->getMockBuilder(Relay::class)->onlyMethods(['waitFrame', 'send'])->getMock();
$relay1
->method('waitFrame')
->willReturnOnConsecutiveCalls(
new Frame('Service.Process{}', [1, 15]),
new Frame('Service.Process{}', [2, 15]),
new Frame('Service.Process{}', [3, 15])
);
$relay1
->method('send')
->withConsecutive(
[new Frame('Service.Process{"Name":"foo","Value":18}', [1, 15], 8)],
[new Frame('Service.Process{"Name":"foo","Value":18}', [2, 15], 8)],
[new Frame('Service.Process{"Name":"foo","Value":18}', [3, 15], 8)]
);

$relay2 = $this->getMockBuilder(Relay::class)->onlyMethods(['waitFrame', 'send'])->getMock();
$relay2
->method('waitFrame')
->willReturnOnConsecutiveCalls(
new Frame('Service.Process{}', [1, 15]),
new Frame('Service.Process{}', [2, 15]),
new Frame('Service.Process{}', [3, 15])
);
$relay2
->method('send')
->withConsecutive(
[new Frame('Service.Process{"Name":"bar","Value":18}', [1, 15], 8)],
[new Frame('Service.Process{"Name":"bar","Value":18}', [2, 15], 8)],
[new Frame('Service.Process{"Name":"bar","Value":18}', [3, 15], 8)]
);

$conn1 = new RPC($relay1);
$conn2 = new RPC($relay2);

$conn1->call('Service.Process', ['Name' => 'foo', 'Value' => 18]);
$conn2->call('Service.Process', ['Name' => 'bar', 'Value' => 18]);
$conn1->call('Service.Process', ['Name' => 'foo', 'Value' => 18]);
$conn2->call('Service.Process', ['Name' => 'bar', 'Value' => 18]);
$conn1->call('Service.Process', ['Name' => 'foo', 'Value' => 18]);
$conn2->call('Service.Process', ['Name' => 'bar', 'Value' => 18]);
}

/**
* @return RPC
*/
Expand Down
Loading