|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Lcobucci\Kafka\Client; |
| 5 | + |
| 6 | +use Closure; |
| 7 | +use Lcobucci\Kafka\Node; |
| 8 | +use Lcobucci\Kafka\Protocol\API\Request; |
| 9 | +use Lcobucci\Kafka\Protocol\API\RequestHeaders; |
| 10 | +use Lcobucci\Kafka\Protocol\Buffer; |
| 11 | +use Lcobucci\Kafka\Protocol\Schema\Parser; |
| 12 | +use Psr\Log\LoggerInterface; |
| 13 | +use React\EventLoop\Loop; |
| 14 | +use React\EventLoop\LoopInterface; |
| 15 | +use React\Promise\Deferred; |
| 16 | +use React\Promise\PromiseInterface; |
| 17 | +use React\Socket\ConnectionInterface; |
| 18 | +use React\Socket\ConnectorInterface; |
| 19 | +use SplPriorityQueue; |
| 20 | +use SplQueue; |
| 21 | +use Throwable; |
| 22 | + |
| 23 | +use function assert; |
| 24 | +use function min; |
| 25 | + |
| 26 | +final class Channel |
| 27 | +{ |
| 28 | + /** @var SplPriorityQueue<int, array{0: array{0: Request, 1: int, 2: string}, 1: Deferred}> */ |
| 29 | + private SplPriorityQueue $processingQueue; |
| 30 | + /** @var SplQueue<array{0: RequestHeaders, 1: Deferred}> */ |
| 31 | + private SplQueue $inFlightQueue; |
| 32 | + private ?ConnectionInterface $connection = null; |
| 33 | + private readonly LoopInterface $loop; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + private ConnectorInterface $connector, |
| 37 | + private LoggerInterface $logger, |
| 38 | + private Parser $schemaParser, |
| 39 | + private Node $node |
| 40 | + ) { |
| 41 | + $this->loop = Loop::get(); |
| 42 | + $this->processingQueue = new SplPriorityQueue(); |
| 43 | + $this->inFlightQueue = new SplQueue(); |
| 44 | + } |
| 45 | + |
| 46 | + public function send(Request $request, int $correlation, string $client): PromiseInterface |
| 47 | + { |
| 48 | + $this->ensureConnected(); |
| 49 | + |
| 50 | + $deferred = new Deferred(); |
| 51 | + $this->processingQueue->insert([[$request, $correlation, $client], $deferred], 0); |
| 52 | + |
| 53 | + return $deferred->promise(); |
| 54 | + } |
| 55 | + |
| 56 | + private function ensureConnected(): void |
| 57 | + { |
| 58 | + if ($this->connection !== null) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + $this->connect(); |
| 63 | + } |
| 64 | + |
| 65 | + private function connect(): void |
| 66 | + { |
| 67 | + $this->logger->info('Opening connection to node', ['node' => $this->node]); |
| 68 | + |
| 69 | + $this->connector->connect($this->node->host . ':' . $this->node->port)->then( |
| 70 | + $this->initializeConnection(...), |
| 71 | + function (Throwable $throwable): void { |
| 72 | + $this->logger->error( |
| 73 | + 'Error while connecting to node #' . $this->node->id, |
| 74 | + ['node' => $this->node, 'exception' => $throwable] |
| 75 | + ); |
| 76 | + |
| 77 | + $this->loop->addTimer(1, $this->connect(...)); |
| 78 | + } |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + public function initializeConnection(ConnectionInterface $connection): void |
| 83 | + { |
| 84 | + $this->logger->info('Connection to node established', ['node' => $this->node]); |
| 85 | + |
| 86 | + $this->connection = $connection; |
| 87 | + $this->connection->on('data', $this->onData(...)); |
| 88 | + $this->connection->on('error', $this->cleanUpConnection(...)); |
| 89 | + $this->connection->on('close', $this->cleanUpConnection(...)); |
| 90 | + |
| 91 | + $this->loop->futureTick($this->processQueue(...)); |
| 92 | + } |
| 93 | + |
| 94 | + public function processQueue(): void |
| 95 | + { |
| 96 | + if (! $this->processingQueue->valid()) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + $this->logger->debug('Processing message queue of node', ['node' => $this->node]); |
| 101 | + |
| 102 | + for ($i = 0, $max = min(15, $this->processingQueue->count()); $i < $max; ++$i) { |
| 103 | + [[$request, $correlation, $client], $deferred] = $this->processingQueue->current(); |
| 104 | + |
| 105 | + $this->processingQueue->next(); |
| 106 | + $headers = $this->sendMessage($request, $correlation, $client); |
| 107 | + $this->inFlightQueue->enqueue([$headers, $deferred]); |
| 108 | + } |
| 109 | + |
| 110 | + $this->loop->futureTick([$this, 'processQueue']); |
| 111 | + } |
| 112 | + |
| 113 | + private function sendMessage(Request $request, int $correlation, string $client): RequestHeaders |
| 114 | + { |
| 115 | + $headers = new RequestHeaders( |
| 116 | + $request->apiKey(), |
| 117 | + $request->highestSupportedVersion(), |
| 118 | + $correlation, |
| 119 | + $client, |
| 120 | + $request->responseClass()::parse(...) |
| 121 | + ); |
| 122 | + |
| 123 | + $header = $headers->toBuffer($this->schemaParser); |
| 124 | + $body = $request->toBuffer($this->schemaParser, $headers->apiVersion); |
| 125 | + |
| 126 | + $length = Buffer::allocate(4); |
| 127 | + $length->writeInt($header->length() + $body->length()); |
| 128 | + |
| 129 | + assert($this->connection instanceof ConnectionInterface); |
| 130 | + $this->connection->write($length->bytes() . $header->bytes() . $body->bytes()); |
| 131 | + |
| 132 | + return $headers; |
| 133 | + } |
| 134 | + |
| 135 | + public function onData(string $data): void |
| 136 | + { |
| 137 | + $this->logger->debug('Message received', ['node' => $this->node]); |
| 138 | + |
| 139 | + [$headers, $deferred] = $this->inFlightQueue->dequeue(); |
| 140 | + |
| 141 | + $buffer = Buffer::fromContent($data); |
| 142 | + $length = $buffer->readInt(); |
| 143 | + |
| 144 | + $deferred->resolve($headers->parseResponse(Buffer::fromContent($buffer->read($length)), $this->schemaParser)); |
| 145 | + } |
| 146 | + |
| 147 | + public function disconnect(): void |
| 148 | + { |
| 149 | + if ($this->connection === null) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + $this->connection->end(); |
| 154 | + } |
| 155 | + |
| 156 | + public function cleanUpConnection(): void |
| 157 | + { |
| 158 | + $this->logger->info('Closing connection to node', ['node' => $this->node]); |
| 159 | + |
| 160 | + $this->connection = null; |
| 161 | + } |
| 162 | +} |
0 commit comments