Skip to content

Commit 708c5b7

Browse files
authored
Merge pull request #112 from php-enqueue/amqp-receive-method-option
[amqp] Add ability to choose what receive method to use: basic_get or basic_consume.
2 parents 7082d1d + b5790eb commit 708c5b7

6 files changed

Lines changed: 159 additions & 81 deletions

File tree

‎pkg/amqp-ext/AmqpConnectionFactory.php‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class AmqpConnectionFactory implements PsrConnectionFactory
3030
* 'connect_timeout' => 'Connection timeout. Note: 0 or greater seconds. May be fractional.',
3131
* 'persisted' => 'bool, Whether it use single persisted connection or open a new one for every context',
3232
* 'lazy' => 'the connection will be performed as later as possible, if the option set to true',
33+
* 'pre_fetch_count' => 'Controls how many messages could be prefetched',
34+
* 'pre_fetch_size' => 'Controls how many messages could be prefetched',
35+
* 'receive_method' => 'Could be either basic_get or basic_consume',
3336
* ]
3437
*
3538
* or
@@ -50,6 +53,22 @@ public function __construct($config = 'amqp://')
5053
}
5154

5255
$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+
}
65+
66+
if ('basic_consume' == $this->config['receive_method']) {
67+
if (false == (version_compare(phpversion('amqp'), '1.9.1', '>=') || phpversion('amqp') == '1.9.1-dev')) {
68+
// @see https://github.com/php-enqueue/enqueue-dev/issues/110 and https://github.com/pdezwart/php-amqp/issues/281
69+
throw new \LogicException('The "basic_consume" method does not work on amqp extension prior 1.9.1 version.');
70+
}
71+
}
5372
}
5473

5574
/**
@@ -62,10 +81,10 @@ public function createContext()
6281
if ($this->config['lazy']) {
6382
return new AmqpContext(function () {
6483
return $this->createExtContext($this->establishConnection());
65-
});
84+
}, $this->config['receive_method']);
6685
}
6786

68-
return new AmqpContext($this->createExtContext($this->establishConnection()));
87+
return new AmqpContext($this->createExtContext($this->establishConnection()), $this->config['receive_method']);
6988
}
7089

7190
/**
@@ -171,6 +190,7 @@ private function defaultConfig()
171190
'lazy' => true,
172191
'pre_fetch_count' => null,
173192
'pre_fetch_size' => null,
193+
'receive_method' => 'basic_get',
174194
];
175195
}
176196
}

‎pkg/amqp-ext/AmqpConsumer.php‎

Lines changed: 85 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,23 @@ class AmqpConsumer implements PsrConsumer
3333
*/
3434
private $isInit;
3535

36+
/**
37+
* @var string
38+
*/
39+
private $receiveMethod;
40+
3641
/**
3742
* @param AmqpContext $context
3843
* @param AmqpQueue $queue
3944
* @param Buffer $buffer
45+
* @param string $receiveMethod
4046
*/
41-
public function __construct(AmqpContext $context, AmqpQueue $queue, Buffer $buffer)
47+
public function __construct(AmqpContext $context, AmqpQueue $queue, Buffer $buffer, $receiveMethod)
4248
{
4349
$this->queue = $queue;
4450
$this->context = $context;
4551
$this->buffer = $buffer;
52+
$this->receiveMethod = $receiveMethod;
4653

4754
$this->isInit = false;
4855
}
@@ -64,61 +71,15 @@ public function getQueue()
6471
*/
6572
public function receive($timeout = 0)
6673
{
67-
// @see https://github.com/php-enqueue/enqueue-dev/issues/110 and https://github.com/pdezwart/php-amqp/issues/281
68-
$end = microtime(true) + ($timeout / 1000);
74+
if ('basic_get' == $this->receiveMethod) {
75+
return $this->receiveBasicGet($timeout);
76+
}
6977

70-
while (0 === $timeout || microtime(true) < $end) {
71-
if ($message = $this->receiveNoWait()) {
72-
return $message;
73-
}
78+
if ('basic_consume' == $this->receiveMethod) {
79+
return $this->receiveBasicConsume($timeout);
7480
}
7581

76-
// if ($this->isInit && $message = $this->buffer->pop($this->getExtQueue()->getConsumerTag())) {
77-
// return $message;
78-
// }
79-
//
80-
// /** @var \AMQPQueue $extQueue */
81-
// $extConnection = $this->getExtQueue()->getChannel()->getConnection();
82-
//
83-
// $originalTimeout = $extConnection->getReadTimeout();
84-
// try {
85-
// $extConnection->setReadTimeout($timeout / 1000);
86-
//
87-
// if (false == $this->isInit) {
88-
// $this->getExtQueue()->consume(null, AMQP_NOPARAM);
89-
//
90-
// $this->isInit = true;
91-
// }
92-
//
93-
// /** @var AmqpMessage|null $message */
94-
// $message = null;
95-
//
96-
// $this->getExtQueue()->consume(function (\AMQPEnvelope $extEnvelope, \AMQPQueue $q) use (&$message) {
97-
// $message = $this->convertMessage($extEnvelope);
98-
// $message->setConsumerTag($q->getConsumerTag());
99-
//
100-
// if ($this->getExtQueue()->getConsumerTag() == $q->getConsumerTag()) {
101-
// return false;
102-
// }
103-
//
104-
// // not our message, put it to buffer and continue.
105-
// $this->buffer->push($q->getConsumerTag(), $message);
106-
//
107-
// $message = null;
108-
//
109-
// return true;
110-
// }, AMQP_JUST_CONSUME);
111-
//
112-
// return $message;
113-
// } catch (\AMQPQueueException $e) {
114-
// if ('Consumer timeout exceed' == $e->getMessage()) {
115-
// return null;
116-
// }
117-
//
118-
// throw $e;
119-
// } finally {
120-
// $extConnection->setReadTimeout($originalTimeout);
121-
// }
82+
throw new \LogicException('The "receiveMethod" is not supported');
12283
}
12384

12485
/**
@@ -160,6 +121,77 @@ public function reject(PsrMessage $message, $requeue = false)
160121
);
161122
}
162123

124+
/**
125+
* @param int $timeout
126+
*
127+
* @return AmqpMessage|null
128+
*/
129+
private function receiveBasicGet($timeout)
130+
{
131+
$end = microtime(true) + ($timeout / 1000);
132+
133+
while (0 === $timeout || microtime(true) < $end) {
134+
if ($message = $this->receiveNoWait()) {
135+
return $message;
136+
}
137+
}
138+
}
139+
140+
/**
141+
* @param int $timeout
142+
*
143+
* @return AmqpMessage|null
144+
*/
145+
private function receiveBasicConsume($timeout)
146+
{
147+
if ($this->isInit && $message = $this->buffer->pop($this->getExtQueue()->getConsumerTag())) {
148+
return $message;
149+
}
150+
151+
/** @var \AMQPQueue $extQueue */
152+
$extConnection = $this->getExtQueue()->getChannel()->getConnection();
153+
154+
$originalTimeout = $extConnection->getReadTimeout();
155+
try {
156+
$extConnection->setReadTimeout($timeout / 1000);
157+
158+
if (false == $this->isInit) {
159+
$this->getExtQueue()->consume(null, AMQP_NOPARAM);
160+
161+
$this->isInit = true;
162+
}
163+
164+
/** @var AmqpMessage|null $message */
165+
$message = null;
166+
167+
$this->getExtQueue()->consume(function (\AMQPEnvelope $extEnvelope, \AMQPQueue $q) use (&$message) {
168+
$message = $this->convertMessage($extEnvelope);
169+
$message->setConsumerTag($q->getConsumerTag());
170+
171+
if ($this->getExtQueue()->getConsumerTag() == $q->getConsumerTag()) {
172+
return false;
173+
}
174+
175+
// not our message, put it to buffer and continue.
176+
$this->buffer->push($q->getConsumerTag(), $message);
177+
178+
$message = null;
179+
180+
return true;
181+
}, AMQP_JUST_CONSUME);
182+
183+
return $message;
184+
} catch (\AMQPQueueException $e) {
185+
if ('Consumer timeout exceed' == $e->getMessage()) {
186+
return null;
187+
}
188+
189+
throw $e;
190+
} finally {
191+
$extConnection->setReadTimeout($originalTimeout);
192+
}
193+
}
194+
163195
/**
164196
* @param \AMQPEnvelope $extEnvelope
165197
*

‎pkg/amqp-ext/AmqpContext.php‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ class AmqpContext implements PsrContext
2525
*/
2626
private $buffer;
2727

28+
/**
29+
* @var string
30+
*/
31+
private $receiveMethod;
32+
2833
/**
2934
* Callable must return instance of \AMQPChannel once called.
3035
*
3136
* @param \AMQPChannel|callable $extChannel
37+
* @param string $receiveMethod
3238
*/
33-
public function __construct($extChannel)
39+
public function __construct($extChannel, $receiveMethod)
3440
{
41+
$this->receiveMethod = $receiveMethod;
42+
3543
if ($extChannel instanceof \AMQPChannel) {
3644
$this->extChannel = $extChannel;
3745
} elseif (is_callable($extChannel)) {
@@ -181,10 +189,10 @@ public function createConsumer(PsrDestination $destination)
181189
$queue = $this->createTemporaryQueue();
182190
$this->bind($destination, $queue);
183191

184-
return new AmqpConsumer($this, $queue, $this->buffer);
192+
return new AmqpConsumer($this, $queue, $this->buffer, $this->receiveMethod);
185193
}
186194

187-
return new AmqpConsumer($this, $destination, $this->buffer);
195+
return new AmqpConsumer($this, $destination, $this->buffer, $this->receiveMethod);
188196
}
189197

190198
public function close()

‎pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public function testThrowIfDsnCouldNotBeParsed()
3737
new AmqpConnectionFactory('amqp://:@/');
3838
}
3939

40+
public function testThrowIfReceiveMenthodIsInvalid()
41+
{
42+
$this->expectException(\LogicException::class);
43+
$this->expectExceptionMessage('Invalid "receive_method" option value "invalidMethod". It could be only "basic_get", "basic_consume"');
44+
45+
new AmqpConnectionFactory(['receive_method' => 'invalidMethod']);
46+
}
47+
4048
/**
4149
* @dataProvider provideConfigs
4250
*
@@ -67,6 +75,7 @@ public static function provideConfigs()
6775
'lazy' => true,
6876
'pre_fetch_count' => null,
6977
'pre_fetch_size' => null,
78+
'receive_method' => 'basic_get',
7079
],
7180
];
7281

@@ -87,6 +96,7 @@ public static function provideConfigs()
8796
'lazy' => true,
8897
'pre_fetch_count' => null,
8998
'pre_fetch_size' => null,
99+
'receive_method' => 'basic_get',
90100
],
91101
];
92102

@@ -105,6 +115,7 @@ public static function provideConfigs()
105115
'lazy' => true,
106116
'pre_fetch_count' => null,
107117
'pre_fetch_size' => null,
118+
'receive_method' => 'basic_get',
108119
],
109120
];
110121

@@ -123,6 +134,7 @@ public static function provideConfigs()
123134
'lazy' => true,
124135
'pre_fetch_count' => null,
125136
'pre_fetch_size' => null,
137+
'receive_method' => 'basic_get',
126138
],
127139
];
128140

@@ -141,6 +153,7 @@ public static function provideConfigs()
141153
'lazy' => '',
142154
'pre_fetch_count' => null,
143155
'pre_fetch_size' => null,
156+
'receive_method' => 'basic_get',
144157
],
145158
];
146159

@@ -159,6 +172,7 @@ public static function provideConfigs()
159172
'lazy' => true,
160173
'pre_fetch_count' => null,
161174
'pre_fetch_size' => null,
175+
'receive_method' => 'basic_get',
162176
],
163177
];
164178

@@ -177,6 +191,7 @@ public static function provideConfigs()
177191
'lazy' => false,
178192
'pre_fetch_count' => null,
179193
'pre_fetch_size' => null,
194+
'receive_method' => 'basic_get',
180195
],
181196
];
182197

@@ -195,6 +210,7 @@ public static function provideConfigs()
195210
'lazy' => true,
196211
'pre_fetch_count' => 123,
197212
'pre_fetch_size' => 321,
213+
'receive_method' => 'basic_get',
198214
],
199215
];
200216

@@ -213,6 +229,7 @@ public static function provideConfigs()
213229
'lazy' => true,
214230
'pre_fetch_count' => 123,
215231
'pre_fetch_size' => 321,
232+
'receive_method' => 'basic_get',
216233
],
217234
];
218235
}

‎pkg/amqp-ext/Tests/AmqpConsumerTest.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function testCouldBeConstructedWithContextAndQueueAndBufferAsArguments()
2424
new AmqpConsumer(
2525
$this->createContext(),
2626
new AmqpQueue('aName'),
27-
new Buffer()
27+
new Buffer(),
28+
'basic_get'
2829
);
2930
}
3031

0 commit comments

Comments
 (0)