Skip to content

Commit ff799ae

Browse files
committed
chore(tests): add RR3 gRPC HTTP worker PoC sample
1 parent 0dee22c commit ff799ae

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
worker-poc.log

tests/php_test_files/rr3/.rr.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: "3"
2+
3+
# RPC plugin: the SDK talks to RoadRunner plugins (KV, Lock, Services, ...) through it.
4+
rpc:
5+
listen: tcp://127.0.0.1:6001
6+
7+
# Application worker configuration.
8+
server:
9+
command: "php worker-poc.php"
10+
11+
# HTTP plugin: accepts incoming requests and forwards them to the PHP worker.
12+
http:
13+
address: 127.0.0.1:8080
14+
pool:
15+
num_workers: 1
16+
17+
logs:
18+
mode: production
19+
level: debug
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "rr3/http",
3+
"type": "project",
4+
"minimum-stability": "dev",
5+
"require": {
6+
"roadrunner/sdk": "dev-master"
7+
},
8+
"require-dev": {
9+
"roxblnfk/unpoly": "^1.8",
10+
"buggregator/trap": "dev-master"
11+
}
12+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Proof-of-concept HTTP worker for the new RoadRunner gRPC interop.
7+
*
8+
* The worker acts as a gRPC *client*: it connects to RoadRunner's
9+
* http.v2.HttpProxyService, pulls one request at a time via FetchRequest,
10+
* and pushes the response back via SendResponse. The response body carries
11+
* the current timestamp so we can prove the rr -> http -> worker round-trip.
12+
*
13+
* Run (from project root):
14+
* ./rr.exe serve -e -c tests/Acceptance/rr.yaml -w tests/Acceptance \
15+
* -o "server.command=php worker-poc.php"
16+
* then hit http://127.0.0.1:8080.
17+
*/
18+
19+
require __DIR__ . '/vendor/autoload.php';
20+
21+
use Google\Protobuf\GPBEmpty;
22+
use RoadRunner\HTTP\DTO\V2\HttpHandlerResponse;
23+
use RoadRunner\HTTP\DTO\V2\HttpHeaderValue;
24+
use RoadRunner\HTTP\DTO\V2\HttpProxyServiceInterface;
25+
use Spiral\Grpc\Client\Config\ConnectionConfig;
26+
use Spiral\Grpc\Client\GrpcClient;
27+
use Spiral\RoadRunner\GRPC\Context;
28+
29+
/** Append a diagnostic line to worker-poc.log next to this file. */
30+
$log =
31+
// /*
32+
tr(...);
33+
/*/
34+
static function (string $message): void {
35+
\file_put_contents(
36+
__DIR__ . '/worker-poc.log',
37+
\sprintf("[%s] %s\n", \date('H:i:s'), $message),
38+
\FILE_APPEND,
39+
);
40+
}; // */
41+
42+
// RoadRunner passes its gRPC endpoint via RR_RPC (e.g. tcp://127.0.0.1:6001).
43+
// ext-grpc wants a bare host:port, so strip the tcp:// scheme.
44+
$rpcAddress = (string) (\getenv('RR_RPC') ?: ($_SERVER['RR_RPC'] ?? 'tcp://127.0.0.1:6001'));
45+
$address = \preg_replace('#^tcp://#', '', $rpcAddress) ?? $rpcAddress;
46+
47+
entry:
48+
try {
49+
$client = GrpcClient::create(new ConnectionConfig($address));
50+
$context = new Context(['metadata' => [], 'options' => []]);
51+
$http = $client->service(HttpProxyServiceInterface::class);
52+
$log(\sprintf('worker started pid=%d address=%s service=%s', \getmypid(), $address, $http::class));
53+
54+
while (true) {
55+
// Long-poll: blocks until RoadRunner has a request for this worker.
56+
$request = $http->FetchRequest($context, new GPBEmpty());
57+
58+
$log(
59+
\sprintf(
60+
'request id=%s %s %s',
61+
$request->getId(),
62+
$request->getMethod(),
63+
$request->getUri(),
64+
),
65+
);
66+
67+
$body = \sprintf(
68+
"Hello from the gRPC PoC worker!\nTime: %s\nRequest ID: %s\n",
69+
\date('c'),
70+
$request->getId(),
71+
);
72+
73+
$response = (new HttpHandlerResponse())
74+
->setId($request->getId())
75+
->setStatus(200)
76+
->setBody($body)
77+
->setHeaders([
78+
'Content-Type' => (new HttpHeaderValue())->setValues(['text/plain; charset=utf-8']),
79+
'X-Powered-By' => (new HttpHeaderValue())->setValues(['rr-grpc-poc']),
80+
]);
81+
82+
// tr($response);
83+
$http->SendResponse($context, $response);
84+
$log('response sent for id=' . $request->getId());
85+
}
86+
} catch (\Throwable $e) {
87+
// tr($e);
88+
$log("error: " . $e->getMessage());
89+
\usleep(1000_000);
90+
// goto entry;
91+
}

0 commit comments

Comments
 (0)