Skip to content

Commit 95c5126

Browse files
author
Pouyan
committed
Adding timeout to RPC call when waiting for replies
1 parent 4e5afe3 commit 95c5126

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,13 @@ public function indexAction($name)
393393
{
394394
$client = $this->get('old_sound_rabbit_mq.integer_store_rpc');
395395
$client->addRequest(serialize(array('min' => 0, 'max' => 10)), 'random_int', 'request_id');
396-
$replies = $client->getReplies();
396+
$timout = 5; // seconds
397+
try {
398+
$replies = $client->getReplies($timeout);
399+
// process $replies['request_id'];
400+
} catch (\PhpAmqpLib\Exception\AMQPTimeoutException $e) {
401+
// handle timeout
402+
}
397403
}
398404
```
399405

RabbitMq/RpcClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public function addRequest($msgBody, $server, $requestId = null, $routingKey = '
3131
$this->requests++;
3232
}
3333

34-
public function getReplies()
34+
public function getReplies($timeout = 0)
3535
{
3636
$this->replies = array();
3737
$this->getChannel()->basic_consume($this->queueName, '', false, true, false, false, array($this, 'processMessage'));
3838

3939
while (count($this->replies) < $this->requests) {
40-
$this->getChannel()->wait();
40+
$this->getChannel()->wait(null, false, $timeout);
4141
}
4242

4343
$this->getChannel()->basic_cancel($this->queueName);

0 commit comments

Comments
 (0)