|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Contributte\RabbitMQ\Consumer; |
| 6 | + |
| 7 | +use Bunny\AbstractClient; |
| 8 | +use Bunny\Channel; |
| 9 | +use Bunny\Client; |
| 10 | +use Bunny\Message; |
| 11 | +use Contributte\RabbitMQ\Consumer\Exception\UnexpectedConsumerResultTypeException; |
| 12 | +use Contributte\RabbitMQ\Queue\IQueue; |
| 13 | + |
| 14 | +class BulkConsumer extends Consumer |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var BulkMessage[] |
| 18 | + */ |
| 19 | + protected array $buffer = []; |
| 20 | + protected int $bulkSize; |
| 21 | + protected int $bulkTime; |
| 22 | + protected ?int $stopTime = null; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + string $name, |
| 26 | + IQueue $queue, |
| 27 | + callable $callback, |
| 28 | + ?int $prefetchSize, |
| 29 | + ?int $prefetchCount, |
| 30 | + int $bulkSize, |
| 31 | + int $bulkTime |
| 32 | + ) { |
| 33 | + parent::__construct($name, $queue, $callback, $prefetchSize, $prefetchCount); |
| 34 | + |
| 35 | + if ($bulkSize > 0 && $bulkTime > 0) { |
| 36 | + $this->bulkSize = $bulkSize; |
| 37 | + $this->bulkTime = $bulkTime; |
| 38 | + } else { |
| 39 | + throw new \InvalidArgumentException("Configuration values bulkSize and bulkTime must have value greater than zero"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public function consume(?int $maxSeconds = null, ?int $maxMessages = null): void |
| 44 | + { |
| 45 | + $this->maxMessages = $maxMessages; |
| 46 | + if ($maxSeconds > 0) { |
| 47 | + $this->stopTime = time() + $maxSeconds; |
| 48 | + } |
| 49 | + |
| 50 | + $channel = $this->queue->getConnection()->getChannel(); |
| 51 | + |
| 52 | + if ($this->prefetchSize !== null || $this->prefetchCount !== null) { |
| 53 | + $channel->qos($this->prefetchSize ?? 0, $this->prefetchCount ?? 0); |
| 54 | + } |
| 55 | + |
| 56 | + $this->setupConsume($channel); |
| 57 | + $this->startConsumeLoop($channel); |
| 58 | + |
| 59 | + //process rest of items |
| 60 | + $this->processBuffer($channel->getClient()); |
| 61 | + } |
| 62 | + |
| 63 | + private function setupConsume(Channel $channel): void |
| 64 | + { |
| 65 | + $channel->consume( |
| 66 | + function (Message $message, Channel $channel, Client $client): void { |
| 67 | + $this->messages++; |
| 68 | + $bulkCount = $this->addToBuffer(new BulkMessage($message, $channel)); |
| 69 | + if ($bulkCount >= $this->bulkSize || $this->isMaxMessages() || $this->isStopTime()) { |
| 70 | + $client->stop(); |
| 71 | + } |
| 72 | + }, |
| 73 | + $this->queue->getName() |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private function startConsumeLoop(Channel $channel): void |
| 78 | + { |
| 79 | + do { |
| 80 | + $channel->getClient()->run($this->getTtl()); |
| 81 | + $this->processBuffer($channel->getClient()); |
| 82 | + } while (!$this->isStopTime() && !$this->isMaxMessages()); |
| 83 | + } |
| 84 | + |
| 85 | + private function addToBuffer(BulkMessage $message): int |
| 86 | + { |
| 87 | + $this->buffer[] = $message; |
| 88 | + |
| 89 | + return count($this->buffer); |
| 90 | + } |
| 91 | + |
| 92 | + private function processBuffer(AbstractClient $client): void |
| 93 | + { |
| 94 | + if (count($this->buffer) === 0) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + $messages = []; |
| 99 | + foreach ($this->buffer as $bulkMessage) { |
| 100 | + $message = $bulkMessage->getMessage(); |
| 101 | + $messages[$message->deliveryTag] = $message; |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + $result = call_user_func($this->callback, $messages); |
| 106 | + } catch (\Throwable $e) { |
| 107 | + $result = array_map(static fn () => IConsumer::MESSAGE_NACK, $messages); |
| 108 | + } |
| 109 | + |
| 110 | + if (!is_array($result)) { |
| 111 | + $result = array_map(static fn () => IConsumer::MESSAGE_NACK, $messages); |
| 112 | + $this->sendResultsBack($client, $result); |
| 113 | + |
| 114 | + throw new UnexpectedConsumerResultTypeException( |
| 115 | + 'Unexpected result from consumer. Expected array(delivery_tag => MESSAGE_STATUS [constant from IConsumer]) but get ' . gettype($result) |
| 116 | + ); |
| 117 | + } |
| 118 | + $result = array_map('intval', $result); |
| 119 | + |
| 120 | + $this->sendResultsBack($client, $result); |
| 121 | + |
| 122 | + $this->buffer = []; |
| 123 | + } |
| 124 | + |
| 125 | + private function sendResultsBack(AbstractClient $client, array $result): void |
| 126 | + { |
| 127 | + if ($client instanceof Client) { |
| 128 | + foreach ($this->buffer as $bulkMessage) { |
| 129 | + $this->sendResponse( |
| 130 | + $bulkMessage->getMessage(), |
| 131 | + $bulkMessage->getChannel(), |
| 132 | + $result[$bulkMessage->getMessage()->deliveryTag] ?? IConsumer::MESSAGE_NACK, |
| 133 | + $client |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private function isStopTime(): bool |
| 140 | + { |
| 141 | + return $this->stopTime !== null && $this->stopTime < time(); |
| 142 | + } |
| 143 | + |
| 144 | + private function getTtl(): int |
| 145 | + { |
| 146 | + if ($this->stopTime > 0) { |
| 147 | + return min($this->bulkTime, $this->stopTime - time()); |
| 148 | + } |
| 149 | + |
| 150 | + return $this->bulkTime; |
| 151 | + } |
| 152 | +} |
0 commit comments