Skip to content

Commit 1199813

Browse files
committed
implement qos
1 parent 497c75b commit 1199813

6 files changed

Lines changed: 103 additions & 19 deletions

File tree

‎pkg/amqp-ext/AmqpContext.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ public function close()
255255
}
256256
}
257257

258+
/**
259+
* {@inheritdoc}
260+
*/
261+
public function setQos($prefetchSize, $prefetchCount, $global)
262+
{
263+
$this->getExtChannel()->qos($prefetchSize, $prefetchCount);
264+
}
265+
258266
/**
259267
* @return \AMQPChannel
260268
*/

‎pkg/amqp-lib/AmqpConnectionFactory.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class AmqpConnectionFactory implements InteropAmqpConnectionFactory
3333
* 'lazy' => 'the connection will be performed as later as possible, if the option set to true',
3434
* 'stream' => 'stream or socket connection',
3535
* 'receive_method' => 'Could be either basic_get or basic_consume',
36+
* 'qos_prefetch_size' => 'The server will send a message in advance if it is equal to or smaller in size than the available prefetch size. May be set to zero, meaning "no specific limit"',
37+
* 'qos_prefetch_count' => 'Specifies a prefetch window in terms of whole messages.',
38+
* 'qos_global' => 'If "false" the QoS settings apply to the current channel only. If this field is "true", they are applied to the entire connection.',
3639
* ]
3740
*
3841
* or
@@ -69,7 +72,7 @@ public function __construct($config = 'amqp://')
6972
*/
7073
public function createContext()
7174
{
72-
return new AmqpContext($this->establishConnection(), $this->config['receive_method']);
75+
return new AmqpContext($this->establishConnection(), $this->config);
7376
}
7477

7578
/**
@@ -224,6 +227,9 @@ private function defaultConfig()
224227
'connection_timeout' => 3.0,
225228
'read_write_timeout' => 3.0,
226229
'receive_method' => 'basic_get',
230+
'qos_prefetch_size' => 0,
231+
'qos_prefetch_count' => 1,
232+
'qos_global' => false,
227233
];
228234
}
229235
}

‎pkg/amqp-lib/AmqpContext.php‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AmqpContext implements InteropAmqpContext
3333
/**
3434
* @var string
3535
*/
36-
private $receiveMethod;
36+
private $config;
3737

3838
/**
3939
* @var Buffer
@@ -42,12 +42,18 @@ class AmqpContext implements InteropAmqpContext
4242

4343
/**
4444
* @param AbstractConnection $connection
45-
* @param string $receiveMethod
45+
* @param array $config
4646
*/
47-
public function __construct(AbstractConnection $connection, $receiveMethod)
47+
public function __construct(AbstractConnection $connection, $config = [])
4848
{
49+
$this->config = array_replace([
50+
'receive_method' => 'basic_get',
51+
'qos_prefetch_size' => 0,
52+
'qos_prefetch_count' => 1,
53+
'qos_global' => false,
54+
], $config);
55+
4956
$this->connection = $connection;
50-
$this->receiveMethod = $receiveMethod;
5157
$this->buffer = new Buffer();
5258
}
5359

@@ -99,10 +105,10 @@ public function createConsumer(PsrDestination $destination)
99105
$queue = $this->createTemporaryQueue();
100106
$this->bind(new AmqpBind($destination, $queue, $queue->getQueueName()));
101107

102-
return new AmqpConsumer($this->getChannel(), $queue, $this->buffer, $this->receiveMethod);
108+
return new AmqpConsumer($this->getChannel(), $queue, $this->buffer, $this->config['receive_method']);
103109
}
104110

105-
return new AmqpConsumer($this->getChannel(), $destination, $this->buffer, $this->receiveMethod);
111+
return new AmqpConsumer($this->getChannel(), $destination, $this->buffer, $this->config['receive_method']);
106112
}
107113

108114
/**
@@ -278,14 +284,26 @@ public function close()
278284
}
279285
}
280286

287+
/**
288+
* {@inheritdoc}
289+
*/
290+
public function setQos($prefetchSize, $prefetchCount, $global)
291+
{
292+
$this->getChannel()->basic_qos($prefetchSize, $prefetchCount, $global);
293+
}
294+
281295
/**
282296
* @return AMQPChannel
283297
*/
284298
private function getChannel()
285299
{
286300
if (null === $this->channel) {
287301
$this->channel = $this->connection->channel();
288-
$this->channel->basic_qos(0, 1, false);
302+
$this->channel->basic_qos(
303+
$this->config['qos_prefetch_size'],
304+
$this->config['qos_prefetch_count'],
305+
$this->config['qos_global']
306+
);
289307
}
290308

291309
return $this->channel;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public static function provideConfigs()
8181
'heartbeat' => 0,
8282
'connection_timeout' => 3.0,
8383
'read_write_timeout' => 3.0,
84+
'qos_prefetch_size' => 0,
85+
'qos_prefetch_count' => 1,
86+
'qos_global' => false,
8487
],
8588
];
8689

@@ -107,6 +110,9 @@ public static function provideConfigs()
107110
'heartbeat' => 0,
108111
'connection_timeout' => 3.0,
109112
'read_write_timeout' => 3.0,
113+
'qos_prefetch_size' => 0,
114+
'qos_prefetch_count' => 1,
115+
'qos_global' => false,
110116
],
111117
];
112118

@@ -131,6 +137,9 @@ public static function provideConfigs()
131137
'heartbeat' => 0,
132138
'connection_timeout' => 3.0,
133139
'read_write_timeout' => 3.0,
140+
'qos_prefetch_size' => 0,
141+
'qos_prefetch_count' => 1,
142+
'qos_global' => false,
134143
],
135144
];
136145

@@ -155,6 +164,9 @@ public static function provideConfigs()
155164
'heartbeat' => 0,
156165
'connection_timeout' => 3.0,
157166
'read_write_timeout' => 3.0,
167+
'qos_prefetch_size' => 0,
168+
'qos_prefetch_count' => 1,
169+
'qos_global' => false,
158170
],
159171
];
160172

@@ -179,6 +191,9 @@ public static function provideConfigs()
179191
'heartbeat' => 0,
180192
'connection_timeout' => '2',
181193
'read_write_timeout' => 3.0,
194+
'qos_prefetch_size' => 0,
195+
'qos_prefetch_count' => 1,
196+
'qos_global' => false,
182197
],
183198
];
184199

@@ -203,6 +218,9 @@ public static function provideConfigs()
203218
'heartbeat' => 0,
204219
'connection_timeout' => 3.0,
205220
'read_write_timeout' => 3.0,
221+
'qos_prefetch_size' => 0,
222+
'qos_prefetch_count' => 1,
223+
'qos_global' => false,
206224
],
207225
];
208226

@@ -227,6 +245,9 @@ public static function provideConfigs()
227245
'heartbeat' => 0,
228246
'connection_timeout' => 3.0,
229247
'read_write_timeout' => 3.0,
248+
'qos_prefetch_size' => 0,
249+
'qos_prefetch_count' => 1,
250+
'qos_global' => false,
230251
],
231252
];
232253

@@ -251,6 +272,9 @@ public static function provideConfigs()
251272
'heartbeat' => 0,
252273
'connection_timeout' => 123,
253274
'read_write_timeout' => 321,
275+
'qos_prefetch_size' => 0,
276+
'qos_prefetch_count' => 1,
277+
'qos_global' => false,
254278
],
255279
];
256280

@@ -275,6 +299,9 @@ public static function provideConfigs()
275299
'heartbeat' => 0,
276300
'connection_timeout' => '123',
277301
'read_write_timeout' => '321',
302+
'qos_prefetch_size' => 0,
303+
'qos_prefetch_count' => 1,
304+
'qos_global' => false,
278305
],
279306
];
280307
}

‎pkg/amqp-lib/Tests/AmqpContextTest.php‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testShouldDeclareTopic()
4747
$topic->addFlag(AmqpTopic::FLAG_INTERNAL);
4848
$topic->addFlag(AmqpTopic::FLAG_AUTODELETE);
4949

50-
$session = new AmqpContext($connection, '');
50+
$session = new AmqpContext($connection);
5151
$session->declareTopic($topic);
5252
}
5353

@@ -77,7 +77,7 @@ public function testShouldDeleteTopic()
7777
$topic->addFlag(AmqpTopic::FLAG_IFUNUSED);
7878
$topic->addFlag(AmqpTopic::FLAG_NOWAIT);
7979

80-
$session = new AmqpContext($connection, '');
80+
$session = new AmqpContext($connection);
8181
$session->deleteTopic($topic);
8282
}
8383

@@ -115,7 +115,7 @@ public function testShouldDeclareQueue()
115115
$queue->addFlag(AmqpQueue::FLAG_EXCLUSIVE);
116116
$queue->addFlag(AmqpQueue::FLAG_NOWAIT);
117117

118-
$session = new AmqpContext($connection, '');
118+
$session = new AmqpContext($connection);
119119
$session->declareQueue($queue);
120120
}
121121

@@ -146,7 +146,7 @@ public function testShouldDeleteQueue()
146146
$queue->addFlag(AmqpQueue::FLAG_IFEMPTY);
147147
$queue->addFlag(AmqpQueue::FLAG_NOWAIT);
148148

149-
$session = new AmqpContext($connection, '');
149+
$session = new AmqpContext($connection);
150150
$session->deleteQueue($queue);
151151
}
152152

@@ -169,7 +169,7 @@ public function testBindShouldBindTopicToTopic()
169169
->willReturn($channel)
170170
;
171171

172-
$context = new AmqpContext($connection, '');
172+
$context = new AmqpContext($connection);
173173
$context->bind(new AmqpBind($target, $source, 'routing-key', 12345));
174174
}
175175

@@ -192,7 +192,7 @@ public function testBindShouldBindTopicToQueue()
192192
->willReturn($channel)
193193
;
194194

195-
$context = new AmqpContext($connection, '');
195+
$context = new AmqpContext($connection);
196196
$context->bind(new AmqpBind($target, $source, 'routing-key', 12345));
197197
$context->bind(new AmqpBind($source, $target, 'routing-key', 12345));
198198
}
@@ -216,7 +216,7 @@ public function testShouldUnBindTopicFromTopic()
216216
->willReturn($channel)
217217
;
218218

219-
$context = new AmqpContext($connection, '');
219+
$context = new AmqpContext($connection);
220220
$context->unbind(new AmqpBind($target, $source, 'routing-key', 12345));
221221
}
222222

@@ -239,7 +239,7 @@ public function testShouldUnBindTopicFromQueue()
239239
->willReturn($channel)
240240
;
241241

242-
$context = new AmqpContext($connection, '');
242+
$context = new AmqpContext($connection);
243243
$context->unbind(new AmqpBind($target, $source, 'routing-key', 12345, ['key' => 'value']));
244244
$context->unbind(new AmqpBind($source, $target, 'routing-key', 12345, ['key' => 'value']));
245245
}
@@ -259,7 +259,7 @@ public function testShouldCloseChannelConnection()
259259
->willReturn($channel)
260260
;
261261

262-
$context = new AmqpContext($connection, '');
262+
$context = new AmqpContext($connection);
263263
$context->createProducer();
264264

265265
$context->close();
@@ -284,10 +284,35 @@ public function testShouldPurgeQueue()
284284
->willReturn($channel)
285285
;
286286

287-
$context = new AmqpContext($connection, '');
287+
$context = new AmqpContext($connection);
288288
$context->purgeQueue($queue);
289289
}
290290

291+
public function testShouldSetQos()
292+
{
293+
$channel = $this->createChannelMock();
294+
$channel
295+
->expects($this->at(0))
296+
->method('basic_qos')
297+
->with($this->identicalTo(0), $this->identicalTo(1), $this->isFalse())
298+
;
299+
$channel
300+
->expects($this->at(1))
301+
->method('basic_qos')
302+
->with($this->identicalTo(123), $this->identicalTo(456), $this->isTrue())
303+
;
304+
305+
$connection = $this->createConnectionMock();
306+
$connection
307+
->expects($this->once())
308+
->method('channel')
309+
->willReturn($channel)
310+
;
311+
312+
$context = new AmqpContext($connection);
313+
$context->setQos(123, 456, true);
314+
}
315+
291316
/**
292317
* @return \PHPUnit_Framework_MockObject_MockObject|AbstractConnection
293318
*/

‎pkg/amqp-lib/Tests/Spec/AmqpContextTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ protected function createContext()
2020
->willReturn($channel)
2121
;
2222

23-
return new AmqpContext($con, '');
23+
return new AmqpContext($con);
2424
}
2525
}

0 commit comments

Comments
 (0)