|
| 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