Skip to content

Commit 2c192e8

Browse files
committed
[amqp][lib] Improve heartbeat handling. Introduce heartbeat on tick.
1 parent 8fbee0e commit 2c192e8

6 files changed

Lines changed: 21 additions & 6 deletions

File tree

‎pkg/amqp-lib/AmqpConnectionFactory.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function __construct($config = 'amqp:')
4747
->addDefaultOption('login_response', null)
4848
->addDefaultOption('locale', 'en_US')
4949
->addDefaultOption('keepalive', false)
50+
->addDefaultOption('heartbeat_on_tick', true)
5051
->parse()
5152
;
5253

‎pkg/amqp-lib/AmqpContext.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function createConsumer(Destination $destination): Consumer
110110
*/
111111
public function createSubscriptionConsumer(): SubscriptionConsumer
112112
{
113-
return new AmqpSubscriptionConsumer($this);
113+
return new AmqpSubscriptionConsumer($this, (bool) $this->config['heartbeat_on_tick']);
114114
}
115115

116116
/**

‎pkg/amqp-lib/AmqpSubscriptionConsumer.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ class AmqpSubscriptionConsumer implements InteropAmqpSubscriptionConsumer
2727
*/
2828
private $subscribers;
2929

30-
public function __construct(AmqpContext $context)
30+
/**
31+
* @var bool
32+
*/
33+
private $heartbeatOnTick;
34+
35+
public function __construct(AmqpContext $context, bool $heartbeatOnTick)
3136
{
3237
$this->context = $context;
38+
$this->heartbeatOnTick = $heartbeatOnTick;
3339
}
3440

3541
public function consume(int $timeout = 0): void
@@ -41,6 +47,12 @@ public function consume(int $timeout = 0): void
4147
$signalHandler = new SignalSocketHelper();
4248
$signalHandler->beforeSocket();
4349

50+
$heartbeatOnTick = function (AmqpContext $context) {
51+
$context->getLibChannel()->getConnection()->getIO()->check_heartbeat();
52+
};
53+
54+
$this->heartbeatOnTick && register_tick_function($heartbeatOnTick);
55+
4456
try {
4557
while (true) {
4658
$start = microtime(true);
@@ -69,6 +81,8 @@ public function consume(int $timeout = 0): void
6981
throw $e;
7082
} finally {
7183
$signalHandler->afterSocket();
84+
85+
$this->heartbeatOnTick && unregister_tick_function($heartbeatOnTick);
7286
}
7387
}
7488

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function testShouldSetQos()
344344

345345
public function testShouldReturnExpectedSubscriptionConsumerInstance()
346346
{
347-
$context = new AmqpContext($this->createConnectionMock(), []);
347+
$context = new AmqpContext($this->createConnectionMock(), ['heartbeat_on_tick' => true]);
348348

349349
$this->assertInstanceOf(AmqpSubscriptionConsumer::class, $context->createSubscriptionConsumer());
350350
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public function testShouldImplementSubscriptionConsumerInterface()
1616
$this->assertTrue($rc->implementsInterface(SubscriptionConsumer::class));
1717
}
1818

19-
public function testCouldBeConstructedWithAmqpContextAsFirstArgument()
19+
public function testCouldBeConstructedWithAmqpContextAndHeartbeatOnTickAsArguments()
2020
{
21-
new AmqpSubscriptionConsumer($this->createAmqpContextMock());
21+
new AmqpSubscriptionConsumer($this->createAmqpContextMock(), $heartbeatOnTick = true);
2222
}
2323

2424
/**

‎pkg/amqp-lib/composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"license": "MIT",
88
"require": {
99
"php": "^7.1.3",
10-
"php-amqplib/php-amqplib": "^2.7",
10+
"php-amqplib/php-amqplib": "^2.8",
1111
"queue-interop/amqp-interop": "^0.8",
1212
"enqueue/amqp-tools": "0.9.x-dev"
1313
},

0 commit comments

Comments
 (0)