Skip to content

Commit 8ea33e3

Browse files
committed
Migrate amqp-ext transport
1 parent 97dd4b9 commit 8ea33e3

19 files changed

Lines changed: 165 additions & 635 deletions

‎pkg/amqp-ext/AmqpConnectionFactory.php‎

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Enqueue\AmqpTools\DelayStrategyAware;
77
use Enqueue\AmqpTools\DelayStrategyAwareTrait;
88
use Interop\Amqp\AmqpConnectionFactory as InteropAmqpConnectionFactory;
9+
use Interop\Queue\PsrContext;
910

1011
class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrategyAware
1112
{
@@ -24,85 +25,52 @@ class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrate
2425
/**
2526
* @see \Enqueue\AmqpTools\ConnectionConfig for possible config formats and values
2627
*
27-
* In addition this factory accepts next options:
28-
* receive_method - Could be either basic_get or basic_consume
29-
*
3028
* @param array|string|null $config
3129
*/
3230
public function __construct($config = 'amqp:')
3331
{
3432
$this->config = (new ConnectionConfig($config))
3533
->addSupportedScheme('amqp+ext')
3634
->addSupportedScheme('amqps+ext')
37-
->addDefaultOption('receive_method', 'basic_get')
3835
->parse()
3936
;
40-
41-
$supportedMethods = ['basic_get', 'basic_consume'];
42-
if (false == in_array($this->config->getOption('receive_method'), $supportedMethods, true)) {
43-
throw new \LogicException(sprintf(
44-
'Invalid "receive_method" option value "%s". It could be only "%s"',
45-
$this->config->getOption('receive_method'),
46-
implode('", "', $supportedMethods)
47-
));
48-
}
49-
50-
if ('basic_consume' == $this->config->getOption('receive_method')) {
51-
if (false == (version_compare(phpversion('amqp'), '1.9.1', '>=') || '1.9.1-dev' == phpversion('amqp'))) {
52-
// @see https://github.com/php-enqueue/enqueue-dev/issues/110 and https://github.com/pdezwart/php-amqp/issues/281
53-
throw new \LogicException('The "basic_consume" method does not work on amqp extension prior 1.9.1 version.');
54-
}
55-
}
5637
}
5738

5839
/**
59-
* {@inheritdoc}
60-
*
6140
* @return AmqpContext
6241
*/
63-
public function createContext()
42+
public function createContext(): PsrContext
6443
{
6544
if ($this->config->isLazy()) {
6645
$context = new AmqpContext(function () {
6746
$extContext = $this->createExtContext($this->establishConnection());
6847
$extContext->qos($this->config->getQosPrefetchSize(), $this->config->getQosPrefetchCount());
6948

7049
return $extContext;
71-
}, $this->config->getOption('receive_method'));
50+
});
7251
$context->setDelayStrategy($this->delayStrategy);
7352

7453
return $context;
7554
}
7655

77-
$context = new AmqpContext($this->createExtContext($this->establishConnection()), $this->config->getOption('receive_method'));
56+
$context = new AmqpContext($this->createExtContext($this->establishConnection()));
7857
$context->setDelayStrategy($this->delayStrategy);
7958
$context->setQos($this->config->getQosPrefetchSize(), $this->config->getQosPrefetchCount(), $this->config->isQosGlobal());
8059

8160
return $context;
8261
}
8362

84-
/**
85-
* @return ConnectionConfig
86-
*/
87-
public function getConfig()
63+
public function getConfig(): ConnectionConfig
8864
{
8965
return $this->config;
9066
}
9167

92-
/**
93-
* @param \AMQPConnection $extConnection
94-
*
95-
* @return \AMQPChannel
96-
*/
97-
private function createExtContext(\AMQPConnection $extConnection)
68+
private function createExtContext(\AMQPConnection $extConnection): \AMQPChannel
9869
{
9970
return new \AMQPChannel($extConnection);
10071
}
10172

102-
/**
103-
* @return \AMQPConnection
104-
*/
105-
private function establishConnection()
73+
private function establishConnection(): \AMQPConnection
10674
{
10775
if (false == $this->connection) {
10876
$extConfig = [];

‎pkg/amqp-ext/AmqpConsumer.php‎

Lines changed: 31 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use Interop\Amqp\AmqpConsumer as InteropAmqpConsumer;
66
use Interop\Amqp\AmqpMessage as InteropAmqpMessage;
7-
use Interop\Amqp\AmqpQueue;
8-
use Interop\Amqp\Impl\AmqpMessage;
7+
use Interop\Amqp\AmqpQueue as InteropAmqpQueue;
98
use Interop\Queue\InvalidMessageException;
109
use Interop\Queue\PsrMessage;
10+
use Interop\Queue\PsrQueue;
1111

1212
class AmqpConsumer implements InteropAmqpConsumer
1313
{
@@ -17,25 +17,15 @@ class AmqpConsumer implements InteropAmqpConsumer
1717
private $context;
1818

1919
/**
20-
* @var AmqpQueue
20+
* @var InteropAmqpQueue
2121
*/
2222
private $queue;
2323

24-
/**
25-
* @var Buffer
26-
*/
27-
private $buffer;
28-
2924
/**
3025
* @var \AMQPQueue
3126
*/
3227
private $extQueue;
3328

34-
/**
35-
* @var string
36-
*/
37-
private $receiveMethod;
38-
3929
/**
4030
* @var int
4131
*/
@@ -46,127 +36,95 @@ class AmqpConsumer implements InteropAmqpConsumer
4636
*/
4737
private $consumerTag;
4838

49-
/**
50-
* @param AmqpContext $context
51-
* @param AmqpQueue $queue
52-
* @param Buffer $buffer
53-
* @param string $receiveMethod
54-
*/
55-
public function __construct(AmqpContext $context, AmqpQueue $queue, Buffer $buffer, $receiveMethod)
39+
public function __construct(AmqpContext $context, InteropAmqpQueue $queue)
5640
{
5741
$this->queue = $queue;
5842
$this->context = $context;
59-
$this->buffer = $buffer;
60-
$this->receiveMethod = $receiveMethod;
6143
$this->flags = self::FLAG_NOPARAM;
6244
}
6345

64-
/**
65-
* {@inheritdoc}
66-
*/
67-
public function setConsumerTag($consumerTag)
46+
public function setConsumerTag(string $consumerTag = null): void
6847
{
6948
$this->consumerTag = $consumerTag;
7049
}
7150

72-
/**
73-
* {@inheritdoc}
74-
*/
75-
public function getConsumerTag()
51+
public function getConsumerTag(): ?string
7652
{
7753
return $this->consumerTag;
7854
}
7955

80-
/**
81-
* {@inheritdoc}
82-
*/
83-
public function clearFlags()
56+
public function clearFlags(): void
8457
{
8558
$this->flags = self::FLAG_NOPARAM;
8659
}
8760

88-
/**
89-
* {@inheritdoc}
90-
*/
91-
public function addFlag($flag)
61+
public function addFlag(int $flag): void
9262
{
9363
$this->flags |= $flag;
9464
}
9565

96-
/**
97-
* {@inheritdoc}
98-
*/
99-
public function getFlags()
66+
public function getFlags(): int
10067
{
10168
return $this->flags;
10269
}
10370

104-
/**
105-
* {@inheritdoc}
106-
*/
107-
public function setFlags($flags)
71+
public function setFlags(int $flags): void
10872
{
10973
$this->flags = $flags;
11074
}
11175

11276
/**
113-
* {@inheritdoc}
114-
*
115-
* @return AmqpQueue
77+
* @return InteropAmqpQueue
11678
*/
117-
public function getQueue()
79+
public function getQueue(): PsrQueue
11880
{
11981
return $this->queue;
12082
}
12183

12284
/**
123-
* {@inheritdoc}
124-
*
125-
* @return InteropAmqpMessage|null
85+
* @return InteropAmqpMessage
12686
*/
127-
public function receive($timeout = 0)
87+
public function receive(int $timeout = 0): ?PsrMessage
12888
{
129-
if ('basic_get' == $this->receiveMethod) {
130-
return $this->receiveBasicGet($timeout);
131-
}
89+
$end = microtime(true) + ($timeout / 1000);
13290

133-
if ('basic_consume' == $this->receiveMethod) {
134-
return $this->receiveBasicConsume($timeout);
91+
while (0 === $timeout || microtime(true) < $end) {
92+
if ($message = $this->receiveNoWait()) {
93+
return $message;
94+
}
95+
96+
usleep(100000); //100ms
13597
}
13698

137-
throw new \LogicException('The "receiveMethod" is not supported');
99+
return null;
138100
}
139101

140102
/**
141-
* {@inheritdoc}
142-
*
143-
* @return AmqpMessage|null
103+
* @return InteropAmqpMessage
144104
*/
145-
public function receiveNoWait()
105+
public function receiveNoWait(): ?PsrMessage
146106
{
147107
if ($extMessage = $this->getExtQueue()->get(Flags::convertConsumerFlags($this->flags))) {
148108
return $this->context->convertMessage($extMessage);
149109
}
110+
111+
return null;
150112
}
151113

152114
/**
153-
* {@inheritdoc}
154-
*
155-
* @param AmqpMessage $message
115+
* @param InteropAmqpMessage $message
156116
*/
157-
public function acknowledge(PsrMessage $message)
117+
public function acknowledge(PsrMessage $message): void
158118
{
159119
InvalidMessageException::assertMessageInstanceOf($message, InteropAmqpMessage::class);
160120

161121
$this->getExtQueue()->ack($message->getDeliveryTag());
162122
}
163123

164124
/**
165-
* {@inheritdoc}
166-
*
167-
* @param AmqpMessage $message
125+
* @param InteropAmqpMessage $message
168126
*/
169-
public function reject(PsrMessage $message, $requeue = false)
127+
public function reject(PsrMessage $message, bool $requeue = false): void
170128
{
171129
InvalidMessageException::assertMessageInstanceOf($message, InteropAmqpMessage::class);
172130

@@ -176,73 +134,7 @@ public function reject(PsrMessage $message, $requeue = false)
176134
);
177135
}
178136

179-
/**
180-
* @param int $timeout
181-
*
182-
* @return InteropAmqpMessage|null
183-
*/
184-
private function receiveBasicGet($timeout)
185-
{
186-
$end = microtime(true) + ($timeout / 1000);
187-
188-
while (0 === $timeout || microtime(true) < $end) {
189-
if ($message = $this->receiveNoWait()) {
190-
return $message;
191-
}
192-
193-
usleep(100000); //100ms
194-
}
195-
}
196-
197-
/**
198-
* @param int $timeout
199-
*
200-
* @return InteropAmqpMessage|null
201-
*/
202-
private function receiveBasicConsume($timeout)
203-
{
204-
if (false == $this->consumerTag) {
205-
$this->context->subscribe($this, function (InteropAmqpMessage $message) {
206-
$this->buffer->push($message->getConsumerTag(), $message);
207-
208-
return false;
209-
});
210-
}
211-
212-
if ($message = $this->buffer->pop($this->consumerTag)) {
213-
return $message;
214-
}
215-
216-
while (true) {
217-
$start = microtime(true);
218-
219-
$this->context->consume($timeout);
220-
221-
if ($message = $this->buffer->pop($this->consumerTag)) {
222-
return $message;
223-
}
224-
225-
// is here when consumed message is not for this consumer
226-
227-
// as timeout is infinite have to continue consumption, but it can overflow message buffer
228-
if ($timeout <= 0) {
229-
continue;
230-
}
231-
232-
// compute remaining timeout and continue until time is up
233-
$stop = microtime(true);
234-
$timeout -= ($stop - $start) * 1000;
235-
236-
if ($timeout <= 0) {
237-
break;
238-
}
239-
}
240-
}
241-
242-
/**
243-
* @return \AMQPQueue
244-
*/
245-
private function getExtQueue()
137+
private function getExtQueue(): \AMQPQueue
246138
{
247139
if (false == $this->extQueue) {
248140
$extQueue = new \AMQPQueue($this->context->getExtChannel());

0 commit comments

Comments
 (0)