Skip to content

Commit 97dd4b9

Browse files
committed
Migrate amqp-lib transport.
1 parent a0b96b7 commit 97dd4b9

16 files changed

Lines changed: 170 additions & 440 deletions

‎pkg/amqp-lib/AmqpConnectionFactory.php‎

Lines changed: 5 additions & 23 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
use PhpAmqpLib\Connection\AbstractConnection;
1011
use PhpAmqpLib\Connection\AMQPLazyConnection;
1112
use PhpAmqpLib\Connection\AMQPLazySocketConnection;
@@ -28,10 +29,7 @@ class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrate
2829
private $connection;
2930

3031
/**
31-
* @see \Enqueue\AmqpTools\ConnectionConfig for possible config formats and values
32-
*
33-
* In addition this factory accepts next options:
34-
* receive_method - Could be either basic_get or basic_consume
32+
* @see \Enqueue\AmqpTools\ConnectionConfig for possible config formats and values.
3533
*
3634
* @param array|string|null $config
3735
*/
@@ -46,43 +44,27 @@ public function __construct($config = 'amqp:')
4644
->addDefaultOption('login_response', null)
4745
->addDefaultOption('locale', 'en_US')
4846
->addDefaultOption('keepalive', false)
49-
->addDefaultOption('receive_method', 'basic_get')
5047
->parse()
5148
;
52-
53-
$supportedMethods = ['basic_get', 'basic_consume'];
54-
if (false == in_array($this->config->getOption('receive_method'), $supportedMethods, true)) {
55-
throw new \LogicException(sprintf(
56-
'Invalid "receive_method" option value "%s". It could be only "%s"',
57-
$this->config->getOption('receive_method'),
58-
implode('", "', $supportedMethods)
59-
));
60-
}
6149
}
6250

6351
/**
6452
* @return AmqpContext
6553
*/
66-
public function createContext()
54+
public function createContext(): PsrContext
6755
{
6856
$context = new AmqpContext($this->establishConnection(), $this->config->getConfig());
6957
$context->setDelayStrategy($this->delayStrategy);
7058

7159
return $context;
7260
}
7361

74-
/**
75-
* @return ConnectionConfig
76-
*/
77-
public function getConfig()
62+
public function getConfig(): ConnectionConfig
7863
{
7964
return $this->config;
8065
}
8166

82-
/**
83-
* @return AbstractConnection
84-
*/
85-
private function establishConnection()
67+
private function establishConnection(): AbstractConnection
8668
{
8769
if (false == $this->connection) {
8870
if ($this->config->getOption('stream')) {

‎pkg/amqp-lib/AmqpConsumer.php‎

Lines changed: 25 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Interop\Amqp\AmqpQueue as InteropAmqpQueue;
88
use Interop\Queue\InvalidMessageException;
99
use Interop\Queue\PsrMessage;
10+
use Interop\Queue\PsrQueue;
1011
use PhpAmqpLib\Channel\AMQPChannel;
1112

1213
class AmqpConsumer implements InteropAmqpConsumer
@@ -26,16 +27,6 @@ class AmqpConsumer implements InteropAmqpConsumer
2627
*/
2728
private $queue;
2829

29-
/**
30-
* @var Buffer
31-
*/
32-
private $buffer;
33-
34-
/**
35-
* @var string
36-
*/
37-
private $receiveMethod;
38-
3930
/**
4031
* @var int
4132
*/
@@ -46,110 +37,86 @@ class AmqpConsumer implements InteropAmqpConsumer
4637
*/
4738
private $consumerTag;
4839

49-
/**
50-
* @param AmqpContext $context
51-
* @param InteropAmqpQueue $queue
52-
* @param Buffer $buffer
53-
* @param string $receiveMethod
54-
*/
55-
public function __construct(AmqpContext $context, InteropAmqpQueue $queue, Buffer $buffer, $receiveMethod)
40+
public function __construct(AmqpContext $context, InteropAmqpQueue $queue)
5641
{
5742
$this->context = $context;
5843
$this->channel = $context->getLibChannel();
5944
$this->queue = $queue;
60-
$this->buffer = $buffer;
61-
$this->receiveMethod = $receiveMethod;
6245
$this->flags = self::FLAG_NOPARAM;
6346
}
6447

65-
/**
66-
* {@inheritdoc}
67-
*/
68-
public function setConsumerTag($consumerTag)
48+
public function setConsumerTag(string $consumerTag = null): void
6949
{
7050
$this->consumerTag = $consumerTag;
7151
}
7252

73-
/**
74-
* {@inheritdoc}
75-
*/
76-
public function getConsumerTag()
53+
public function getConsumerTag(): ?string
7754
{
7855
return $this->consumerTag;
7956
}
8057

81-
/**
82-
* {@inheritdoc}
83-
*/
84-
public function clearFlags()
58+
public function clearFlags(): void
8559
{
8660
$this->flags = self::FLAG_NOPARAM;
8761
}
8862

89-
/**
90-
* {@inheritdoc}
91-
*/
92-
public function addFlag($flag)
63+
public function addFlag(int $flag): void
9364
{
9465
$this->flags |= $flag;
9566
}
9667

97-
/**
98-
* {@inheritdoc}
99-
*/
100-
public function getFlags()
68+
public function getFlags(): int
10169
{
10270
return $this->flags;
10371
}
10472

105-
/**
106-
* {@inheritdoc}
107-
*/
108-
public function setFlags($flags)
73+
public function setFlags(int $flags): void
10974
{
11075
$this->flags = $flags;
11176
}
11277

11378
/**
11479
* @return InteropAmqpQueue
11580
*/
116-
public function getQueue()
81+
public function getQueue(): PsrQueue
11782
{
11883
return $this->queue;
11984
}
12085

12186
/**
122-
* {@inheritdoc}
123-
*
124-
* @return InteropAmqpMessage|null
87+
* @return InteropAmqpMessage
12588
*/
126-
public function receive($timeout = 0)
89+
public function receive(int $timeout = 0): ?PsrMessage
12790
{
128-
if ('basic_get' == $this->receiveMethod) {
129-
return $this->receiveBasicGet($timeout);
130-
}
91+
$end = microtime(true) + ($timeout / 1000);
92+
93+
while (0 === $timeout || microtime(true) < $end) {
94+
if ($message = $this->receiveNoWait()) {
95+
return $message;
96+
}
13197

132-
if ('basic_consume' == $this->receiveMethod) {
133-
return $this->receiveBasicConsume($timeout);
98+
usleep(100000); //100ms
13499
}
135100

136-
throw new \LogicException('The "receiveMethod" is not supported');
101+
return null;
137102
}
138103

139104
/**
140-
* @return InteropAmqpMessage|null
105+
* @return InteropAmqpMessage
141106
*/
142-
public function receiveNoWait()
107+
public function receiveNoWait(): ?PsrMessage
143108
{
144109
if ($message = $this->channel->basic_get($this->queue->getQueueName(), (bool) ($this->getFlags() & InteropAmqpConsumer::FLAG_NOACK))) {
145110
return $this->context->convertMessage($message);
146111
}
112+
113+
return null;
147114
}
148115

149116
/**
150117
* @param InteropAmqpMessage $message
151118
*/
152-
public function acknowledge(PsrMessage $message)
119+
public function acknowledge(PsrMessage $message): void
153120
{
154121
InvalidMessageException::assertMessageInstanceOf($message, InteropAmqpMessage::class);
155122

@@ -160,73 +127,10 @@ public function acknowledge(PsrMessage $message)
160127
* @param InteropAmqpMessage $message
161128
* @param bool $requeue
162129
*/
163-
public function reject(PsrMessage $message, $requeue = false)
130+
public function reject(PsrMessage $message, bool $requeue = false): void
164131
{
165132
InvalidMessageException::assertMessageInstanceOf($message, InteropAmqpMessage::class);
166133

167134
$this->channel->basic_reject($message->getDeliveryTag(), $requeue);
168135
}
169-
170-
/**
171-
* @param int $timeout
172-
*
173-
* @return InteropAmqpMessage|null
174-
*/
175-
private function receiveBasicGet($timeout)
176-
{
177-
$end = microtime(true) + ($timeout / 1000);
178-
179-
while (0 === $timeout || microtime(true) < $end) {
180-
if ($message = $this->receiveNoWait()) {
181-
return $message;
182-
}
183-
184-
usleep(100000); //100ms
185-
}
186-
}
187-
188-
/**
189-
* @param int $timeout
190-
*
191-
* @return InteropAmqpMessage|null
192-
*/
193-
private function receiveBasicConsume($timeout)
194-
{
195-
if (false == $this->consumerTag) {
196-
$this->context->subscribe($this, function (InteropAmqpMessage $message) {
197-
$this->buffer->push($message->getConsumerTag(), $message);
198-
199-
return false;
200-
});
201-
}
202-
203-
if ($message = $this->buffer->pop($this->consumerTag)) {
204-
return $message;
205-
}
206-
207-
while (true) {
208-
$start = microtime(true);
209-
210-
$this->context->consume($timeout);
211-
212-
if ($message = $this->buffer->pop($this->consumerTag)) {
213-
return $message;
214-
}
215-
216-
// is here when consumed message is not for this consumer
217-
218-
// as timeout is infinite have to continue consumption, but it can overflow message buffer
219-
if ($timeout <= 0) {
220-
continue;
221-
}
222-
223-
// compute remaining timeout and continue until time is up
224-
$stop = microtime(true);
225-
$timeout -= ($stop - $start) * 1000;
226-
227-
if ($timeout <= 0) {
228-
break;
229-
}
230-
}
231-
}
232136
}

0 commit comments

Comments
 (0)