Skip to content

Commit 7616782

Browse files
committed
consumer basic consume impl
1 parent 087578e commit 7616782

4 files changed

Lines changed: 184 additions & 13 deletions

File tree

‎pkg/amqp-lib/AmqpConnectionFactory.php‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class AmqpConnectionFactory implements PsrConnectionFactory
3232
* 'pass' => 'amqp.password Password. Note: Max 128 characters.',
3333
* 'lazy' => 'the connection will be performed as later as possible, if the option set to true',
3434
* 'stream' => 'stream or socket connection',
35+
* 'receive_method' => 'Could be either basic_get or basic_consume',
3536
* ]
3637
*
3738
* or
@@ -52,14 +53,23 @@ public function __construct($config = 'amqp://')
5253
}
5354

5455
$this->config = array_replace($this->defaultConfig(), $config);
56+
57+
$supportedMethods = ['basic_get', 'basic_consume'];
58+
if (false == in_array($this->config['receive_method'], $supportedMethods, true)) {
59+
throw new \LogicException(sprintf(
60+
'Invalid "receive_method" option value "%s". It could be only "%s"',
61+
$this->config['receive_method'],
62+
implode('", "', $supportedMethods)
63+
));
64+
}
5565
}
5666

5767
/**
5868
* @return AmqpContext
5969
*/
6070
public function createContext()
6171
{
62-
return new AmqpContext($this->establishConnection());
72+
return new AmqpContext($this->establishConnection(), $this->config['receive_method']);
6373
}
6474

6575
/**
@@ -214,6 +224,7 @@ private function defaultConfig()
214224
'heartbeat' => 0,
215225
'connection_timeout' => 3.0,
216226
'read_write_timeout' => 3.0,
227+
'receive_method' => 'basic_get',
217228
];
218229
}
219230
}

‎pkg/amqp-lib/AmqpConsumer.php‎

Lines changed: 115 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Enqueue\AmqpLib;
44

5+
use Interop\Queue\Exception;
56
use Interop\Queue\InvalidMessageException;
67
use Interop\Queue\PsrConsumer;
78
use Interop\Queue\PsrMessage;
89
use PhpAmqpLib\Channel\AMQPChannel;
10+
use PhpAmqpLib\Exception\AMQPTimeoutException;
911
use PhpAmqpLib\Message\AMQPMessage as LibAMQPMessage;
1012
use PhpAmqpLib\Wire\AMQPTable;
1113

@@ -21,14 +23,45 @@ class AmqpConsumer implements PsrConsumer
2123
*/
2224
private $queue;
2325

26+
/**
27+
* @var Buffer
28+
*/
29+
private $buffer;
30+
31+
/**
32+
* @var bool
33+
*/
34+
private $isInit;
35+
36+
/**
37+
* @var string
38+
*/
39+
private $receiveMethod;
40+
41+
/**
42+
* @var AmqpMessage
43+
*/
44+
private $receivedMessage;
45+
46+
/**
47+
* @var string
48+
*/
49+
private $consumerTag;
50+
2451
/**
2552
* @param AMQPChannel $channel
2653
* @param AmqpQueue $queue
54+
* @param Buffer $buffer
55+
* @param string $receiveMethod
2756
*/
28-
public function __construct(AMQPChannel $channel, AmqpQueue $queue)
57+
public function __construct(AMQPChannel $channel, AmqpQueue $queue, Buffer $buffer, $receiveMethod)
2958
{
3059
$this->channel = $channel;
3160
$this->queue = $queue;
61+
$this->buffer = $buffer;
62+
$this->receiveMethod = $receiveMethod;
63+
64+
$this->isInit = false;
3265
}
3366

3467
/**
@@ -40,21 +73,21 @@ public function getQueue()
4073
}
4174

4275
/**
43-
* @param int $timeout
76+
* {@inheritdoc}
4477
*
4578
* @return AmqpMessage|null
4679
*/
4780
public function receive($timeout = 0)
4881
{
49-
$end = microtime(true) + ($timeout / 1000);
50-
51-
while (0 === $timeout || microtime(true) < $end) {
52-
if ($message = $this->receiveNoWait()) {
53-
return $message;
54-
}
82+
if ('basic_get' == $this->receiveMethod) {
83+
return $this->receiveBasicGet($timeout);
84+
}
5585

56-
usleep(100000); //100ms
86+
if ('basic_consume' == $this->receiveMethod) {
87+
return $this->receiveBasicConsume($timeout);
5788
}
89+
90+
throw new \LogicException('The "receiveMethod" is not supported');
5891
}
5992

6093
/**
@@ -110,4 +143,77 @@ private function convertMessage(LibAMQPMessage $amqpMessage)
110143

111144
return $message;
112145
}
146+
147+
/**
148+
* @param int $timeout
149+
*
150+
* @return AmqpMessage|null
151+
*/
152+
private function receiveBasicGet($timeout)
153+
{
154+
$end = microtime(true) + ($timeout / 1000);
155+
156+
while (0 === $timeout || microtime(true) < $end) {
157+
if ($message = $this->receiveNoWait()) {
158+
return $message;
159+
}
160+
161+
usleep(100000); //100ms
162+
}
163+
}
164+
165+
/**
166+
* @param int $timeout
167+
*
168+
* @return AmqpMessage|null
169+
*/
170+
private function receiveBasicConsume($timeout)
171+
{
172+
if (false === $this->isInit) {
173+
$callback = function (LibAMQPMessage $message) {
174+
$receivedMessage = $this->convertMessage($message);
175+
$consumerTag = $message->delivery_info['consumer_tag'];
176+
177+
if ($this->consumerTag === $consumerTag) {
178+
$this->receivedMessage = $receivedMessage;
179+
} else {
180+
// not our message, put it to buffer and continue.
181+
$this->buffer->push($consumerTag, $receivedMessage);
182+
}
183+
};
184+
185+
$this->channel->basic_qos(0, 1, false);
186+
187+
$consumerTag = $this->channel->basic_consume(
188+
$this->queue->getQueueName(),
189+
$this->queue->getConsumerTag(),
190+
$this->queue->isNoLocal(),
191+
$this->queue->isNoAck(),
192+
$this->queue->isExclusive(),
193+
$this->queue->isNoWait(),
194+
$callback
195+
);
196+
197+
$this->consumerTag = $consumerTag ?: $this->queue->getConsumerTag();
198+
199+
if (empty($this->consumerTag)) {
200+
throw new Exception('Got empty consumer tag');
201+
}
202+
203+
$this->isInit = true;
204+
}
205+
206+
if ($message = $this->buffer->pop($this->consumerTag)) {
207+
return $message;
208+
}
209+
210+
$this->receivedMessage = null;
211+
212+
try {
213+
$this->channel->wait(null, false, $timeout);
214+
} catch (AMQPTimeoutException $e) {
215+
}
216+
217+
return $this->receivedMessage;
218+
}
113219
}

‎pkg/amqp-lib/AmqpContext.php‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,25 @@ class AmqpContext implements PsrContext
2323
*/
2424
private $channel;
2525

26+
/**
27+
* @var string
28+
*/
29+
private $receiveMethod;
30+
31+
/**
32+
* @var Buffer
33+
*/
34+
private $buffer;
35+
2636
/**
2737
* @param AbstractConnection $connection
38+
* @param string $receiveMethod
2839
*/
29-
public function __construct(AbstractConnection $connection)
40+
public function __construct(AbstractConnection $connection, $receiveMethod)
3041
{
3142
$this->connection = $connection;
43+
$this->receiveMethod = $receiveMethod;
44+
$this->buffer = new Buffer();
3245
}
3346

3447
/**
@@ -79,10 +92,10 @@ public function createConsumer(PsrDestination $destination)
7992
$queue = $this->createTemporaryQueue();
8093
$this->bind($destination, $queue);
8194

82-
return new AmqpConsumer($this->getChannel(), $queue);
95+
return new AmqpConsumer($this->getChannel(), $queue, $this->buffer, $this->receiveMethod);
8396
}
8497

85-
return new AmqpConsumer($this->getChannel(), $destination);
98+
return new AmqpConsumer($this->getChannel(), $destination, $this->buffer, $this->receiveMethod);
8699
}
87100

88101
/**

‎pkg/amqp-lib/Buffer.php‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpLib;
4+
5+
class Buffer
6+
{
7+
/**
8+
* @var array ['aTag' => [AmqpMessage, AmqpMessage ...]]
9+
*/
10+
private $messages;
11+
12+
public function __construct()
13+
{
14+
$this->messages = [];
15+
}
16+
17+
/**
18+
* @param string $consumerTag
19+
* @param AmqpMessage $message
20+
*/
21+
public function push($consumerTag, AmqpMessage $message)
22+
{
23+
if (false == array_key_exists($consumerTag, $this->messages)) {
24+
$this->messages[$consumerTag] = [];
25+
}
26+
27+
$this->messages[$consumerTag][] = $message;
28+
}
29+
30+
/**
31+
* @param string $consumerTag
32+
*
33+
* @return AmqpMessage|null
34+
*/
35+
public function pop($consumerTag)
36+
{
37+
if (false == empty($this->messages[$consumerTag])) {
38+
return array_shift($this->messages[$consumerTag]);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)