Skip to content

Commit a8a75f9

Browse files
committed
fix concurrent consumption
1 parent 2e83157 commit a8a75f9

3 files changed

Lines changed: 28 additions & 27 deletions

File tree

‎pkg/amqp-lib/AmqpConsumer.php‎

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ class AmqpConsumer implements InteropAmqpConsumer
4141
*/
4242
private $receiveMethod;
4343

44-
/**
45-
* @var InteropAmqpMessage
46-
*/
47-
private $receivedMessage;
48-
4944
/**
5045
* @var int
5146
*/
@@ -164,8 +159,6 @@ public function acknowledge(PsrMessage $message)
164159
{
165160
InvalidMessageException::assertMessageInstanceOf($message, InteropAmqpMessage::class);
166161

167-
var_dump($message->getDeliveryTag());
168-
169162
$this->channel->basic_ack($message->getDeliveryTag());
170163
}
171164

@@ -233,16 +226,9 @@ private function receiveBasicConsume($timeout)
233226
$receivedMessage = $this->convertMessage($message);
234227
$receivedMessage->setConsumerTag($message->delivery_info['consumer_tag']);
235228

236-
if ($this->consumerTag === $receivedMessage->getConsumerTag()) {
237-
$this->receivedMessage = $receivedMessage;
238-
} else {
239-
// not our message, put it to buffer and continue.
240-
$this->buffer->push($receivedMessage->getConsumerTag(), $receivedMessage);
241-
}
229+
$this->buffer->push($receivedMessage->getConsumerTag(), $receivedMessage);
242230
};
243231

244-
$this->channel->basic_qos(0, 1, false);
245-
246232
$consumerTag = $this->channel->basic_consume(
247233
$this->queue->getQueueName(),
248234
$this->getConsumerTag() ?: $this->getQueue()->getConsumerTag(),
@@ -253,7 +239,7 @@ private function receiveBasicConsume($timeout)
253239
$callback
254240
);
255241

256-
$this->consumerTag = $consumerTag ?: $this->queue->getConsumerTag();
242+
$this->consumerTag = $consumerTag ?: $this->getQueue()->getConsumerTag();
257243

258244
if (empty($this->consumerTag)) {
259245
throw new Exception('Got empty consumer tag');
@@ -266,16 +252,31 @@ private function receiveBasicConsume($timeout)
266252
return $message;
267253
}
268254

269-
$this->receivedMessage = null;
270-
271255
try {
272-
echo 'here', PHP_EOL;
273-
$this->channel->wait(null, false, $timeout);
274-
echo 'here1', PHP_EOL;
275-
} catch (AMQPTimeoutException $e) {
276-
echo 'here2', PHP_EOL;
277-
}
256+
while (true) {
257+
$start = microtime(true);
258+
259+
$this->channel->wait(null, false, $timeout / 1000);
278260

279-
return $this->receivedMessage;
261+
if ($message = $this->buffer->pop($this->consumerTag)) {
262+
return $message;
263+
}
264+
265+
// is here when consumed message is not for this consumer
266+
267+
// as timeout is infinite have to continue consumption, but it can overflow message buffer
268+
if ($timeout <= 0) {
269+
continue;
270+
}
271+
272+
// compute remaining timeout and continue until time is up
273+
$stop = microtime(true);
274+
$timeout -= ($stop - $start) * 1000;
275+
276+
if ($timeout <= 0) {
277+
break;
278+
}
279+
}
280+
} catch (AMQPTimeoutException $e) {}
280281
}
281282
}

‎pkg/amqp-lib/AmqpContext.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ private function getChannel()
285285
{
286286
if (null === $this->channel) {
287287
$this->channel = $this->connection->channel();
288+
$this->channel->basic_qos(0, 1, false);
288289
}
289290

290291
return $this->channel;

‎pkg/amqp-lib/examples/consume.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@
3434

3535
$queue = $context->createQueue('bar');
3636
$barConsumer = $context->createConsumer($queue);
37-
//$barConsumer->addFlag(\Interop\Amqp\AmqpConsumer::FLAG_NOACK);
3837

3938
$consumers = [$fooConsumer, $barConsumer];
4039

4140
$consumer = $consumers[rand(0, 1)];
4241

4342
while (true) {
44-
if ($m = $consumer->receive(0.100)) {
43+
if ($m = $consumer->receive(100)) {
4544
echo $m->getBody(), PHP_EOL;
4645
$consumer->acknowledge($m);
4746
}

0 commit comments

Comments
 (0)