Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[amqp][lib] Improve heartbeat handling. Introduce heartbeat on tick. Fixes "Invalid frame type 65" and "Broken pipe or closed connection" #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 pkg/amqp-lib/AmqpConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct($config = 'amqp:')
->addDefaultOption('login_response', null)
->addDefaultOption('locale', 'en_US')
->addDefaultOption('keepalive', false)
->addDefaultOption('heartbeat_on_tick', true)
->parse()
;

Expand Down
2 changes: 1 addition & 1 deletion 2 pkg/amqp-lib/AmqpContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function createConsumer(Destination $destination): Consumer
*/
public function createSubscriptionConsumer(): SubscriptionConsumer
{
return new AmqpSubscriptionConsumer($this);
return new AmqpSubscriptionConsumer($this, (bool) $this->config['heartbeat_on_tick']);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion 16 pkg/amqp-lib/AmqpSubscriptionConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ class AmqpSubscriptionConsumer implements InteropAmqpSubscriptionConsumer
*/
private $subscribers;

public function __construct(AmqpContext $context)
/**
* @var bool
*/
private $heartbeatOnTick;

public function __construct(AmqpContext $context, bool $heartbeatOnTick)
{
$this->context = $context;
$this->heartbeatOnTick = $heartbeatOnTick;
}

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

$heartbeatOnTick = function (AmqpContext $context) {
$context->getLibChannel()->getConnection()->getIO()->check_heartbeat();
};

$this->heartbeatOnTick && register_tick_function($heartbeatOnTick, $this->context);

try {
while (true) {
$start = microtime(true);
Expand Down Expand Up @@ -69,6 +81,8 @@ public function consume(int $timeout = 0): void
throw $e;
} finally {
$signalHandler->afterSocket();

$this->heartbeatOnTick && unregister_tick_function($heartbeatOnTick);
}
}

Expand Down
2 changes: 1 addition & 1 deletion 2 pkg/amqp-lib/Tests/AmqpContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public function testShouldSetQos()

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

$this->assertInstanceOf(AmqpSubscriptionConsumer::class, $context->createSubscriptionConsumer());
}
Expand Down
4 changes: 2 additions & 2 deletions 4 pkg/amqp-lib/Tests/AmqpSubscriptionConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function testShouldImplementSubscriptionConsumerInterface()
$this->assertTrue($rc->implementsInterface(SubscriptionConsumer::class));
}

public function testCouldBeConstructedWithAmqpContextAsFirstArgument()
public function testCouldBeConstructedWithAmqpContextAndHeartbeatOnTickAsArguments()
{
new AmqpSubscriptionConsumer($this->createAmqpContextMock());
new AmqpSubscriptionConsumer($this->createAmqpContextMock(), $heartbeatOnTick = true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Enqueue\AmqpLib\Tests\Functional;

use Enqueue\AmqpLib\AmqpConnectionFactory;
use Enqueue\AmqpLib\AmqpContext;
use Interop\Amqp\AmqpQueue;
use Interop\Queue\Consumer;
use Interop\Queue\Context;
use Interop\Queue\Message;
use PHPUnit\Framework\TestCase;

/**
* @group functional
*/
class AmqpSubscriptionConsumerWithHeartbeatOnTickTest extends TestCase
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where this test actually tests anything related to ticks. The test would also pass if no tick handler was registered. Or am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It contains decalre tricks which triggers tick callback and therfor heartbeat logic.

Is there a better way to test it?

{
/**
* @var Context
*/
private $context;

protected function tearDown()
{
if ($this->context) {
$this->context->close();
}

parent::tearDown();
}

public function test()
{
$this->context = $context = $this->createContext();

$fooQueue = $this->createQueue($context, 'foo_subscription_consumer_consume_from_all_subscribed_queues_spec');

$expectedFooBody = 'fooBody';

$context->createProducer()->send($fooQueue, $context->createMessage($expectedFooBody));

$fooConsumer = $context->createConsumer($fooQueue);

$actualBodies = [];
$actualQueues = [];
$callback = function (Message $message, Consumer $consumer) use (&$actualBodies, &$actualQueues) {
declare(ticks=1) {
$actualBodies[] = $message->getBody();
$actualQueues[] = $consumer->getQueue()->getQueueName();

$consumer->acknowledge($message);

return true;
}
};

$subscriptionConsumer = $context->createSubscriptionConsumer();
$subscriptionConsumer->subscribe($fooConsumer, $callback);

$subscriptionConsumer->consume(1000);

$this->assertCount(1, $actualBodies);
}

protected function createContext(): AmqpContext
{
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));

$context = $factory->createContext();
$context->setQos(0, 5, false);

return $context;
}

protected function createQueue(AmqpContext $context, string $queueName): AmqpQueue
{
$queue = $context->createQueue($queueName);
$context->declareQueue($queue);
$context->purgeQueue($queue);

return $queue;
}
}
2 changes: 1 addition & 1 deletion 2 pkg/amqp-lib/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"require": {
"php": "^7.1.3",
"php-amqplib/php-amqplib": "^2.7",
"php-amqplib/php-amqplib": "^2.8",
"queue-interop/amqp-interop": "^0.8",
"enqueue/amqp-tools": "0.9.x-dev"
},
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.