Repository navigation
Heartbeat every open AMQP connection, not just the last one used #30
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
Open
TomK
wants to merge
10
commits into
master
Choose a base branch
from
heartbeat-all-connections
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+485
−45
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
329eb0a
Test that idle AMQP connections keep heartbeating
TomK afbb3d8
Heartbeat every open AMQP connection from one SIGALRM handler
TomK 54758a3
Test heartbeat registry edge cases from review
TomK a1e9693
Keep heartbeat registry from leaking, stalling or hiding failures
TomK d19b6cc
Collect twice before checking the released provider is freed
TomK 62fee4b
Test paused consumer callbacks and single-mode disconnects
TomK 9700c84
Leave deliveries for redelivery when the consumer heartbeat failed
TomK bc000b4
Test that a batch drops deliveries from a lost consume connection
TomK 72ac06c
Sleep to a deadline in heartbeat tests; fix the mid-batch assertion
TomK 822e625
Clear the pending batch when the consume connection disconnects
TomK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| <?php | ||
| namespace Packaged\Queue\Provider\Amqp; | ||
|
|
||
| use PhpAmqpLib\Connection\AbstractConnection; | ||
|
|
||
| /** | ||
| * Sends heartbeats for every registered AMQP connection in the process from one SIGALRM handler. | ||
| * | ||
| * A process has a single SIGALRM handler, so one sender per connection would leave every | ||
| * connection but the last registered without heartbeats. | ||
| * | ||
| * Connections are held by weak reference, so the registry never keeps a provider alive. | ||
| */ | ||
| final class AmqpHeartbeat | ||
| { | ||
| /** @var \WeakReference[] keyed by spl_object_id */ | ||
| private static $_connections = []; | ||
| /** @var bool[] connections whose heartbeat failed, keyed by spl_object_id */ | ||
| private static $_failed = []; | ||
| private static $_handlerInstalled = false; | ||
| /** Unix time the pending alarm fires, or 0 when none is pending */ | ||
| private static $_nextBeat = 0; | ||
|
|
||
| public static function isSupported(): bool | ||
| { | ||
| if(defined('AMQP_WITHOUT_SIGNALS') && AMQP_WITHOUT_SIGNALS) | ||
| { | ||
| return false; | ||
| } | ||
| return function_exists('pcntl_async_signals') | ||
| && function_exists('pcntl_signal') | ||
| && function_exists('pcntl_alarm') | ||
| && PHP_OS_FAMILY !== 'Windows'; | ||
| } | ||
|
|
||
| public static function register(AbstractConnection $connection): void | ||
| { | ||
| if(!self::isSupported() || $connection->getHeartbeat() < 1) | ||
| { | ||
| return; | ||
| } | ||
| $id = spl_object_id($connection); | ||
| self::$_connections[$id] = \WeakReference::create($connection); | ||
| unset(self::$_failed[$id]); | ||
| self::_schedule(); | ||
| } | ||
|
|
||
| public static function unregister(AbstractConnection $connection): void | ||
| { | ||
| $id = spl_object_id($connection); | ||
| if(!self::_holds($id, $connection)) | ||
| { | ||
| return; | ||
| } | ||
| unset(self::$_connections[$id], self::$_failed[$id]); | ||
| if(!self::$_connections) | ||
| { | ||
| self::_cancel(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Whether a heartbeat on this connection failed in the signal handler. | ||
| * The connection is unusable, so its owner should disconnect and reconnect. | ||
| */ | ||
| public static function hasFailed(AbstractConnection $connection): bool | ||
| { | ||
| $id = spl_object_id($connection); | ||
| return isset(self::$_failed[$id]) && self::_holds($id, $connection); | ||
| } | ||
|
|
||
| /** | ||
| * Heartbeat each idle connection. Runs inside the SIGALRM handler. | ||
| */ | ||
| public static function beat(): void | ||
| { | ||
| self::$_nextBeat = 0; | ||
| foreach(self::$_connections as $id => $reference) | ||
| { | ||
| $connection = $reference->get(); | ||
| if($connection === null || !$connection->isConnected()) | ||
| { | ||
| unset(self::$_connections[$id], self::$_failed[$id]); | ||
| continue; | ||
| } | ||
| // A heartbeat frame written in the middle of another frame would corrupt the stream. | ||
| if(isset(self::$_failed[$id]) || $connection->isWriting()) | ||
| { | ||
| continue; | ||
| } | ||
| if(time() > $connection->getLastActivity() + self::_interval($connection)) | ||
| { | ||
| try | ||
| { | ||
| $connection->checkHeartBeat(); | ||
| } | ||
| catch(\Throwable $e) | ||
| { | ||
| // Throwing here would unwind whatever code the signal interrupted. | ||
| // The owning provider sees the failure on its next push or consume and reconnects. | ||
| self::$_failed[$id] = true; | ||
|
TomK marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
| } | ||
| self::_schedule(); | ||
| } | ||
|
|
||
| private static function _holds(int $id, AbstractConnection $connection): bool | ||
| { | ||
| return isset(self::$_connections[$id]) && self::$_connections[$id]->get() === $connection; | ||
| } | ||
|
|
||
| private static function _interval(AbstractConnection $connection): int | ||
| { | ||
| return (int)ceil($connection->getHeartbeat() / 2); | ||
| } | ||
|
|
||
| /** | ||
| * Arm the alarm for the shortest interval among live connections. | ||
| * A pending alarm is only ever brought forward, so registry changes cannot postpone it. | ||
| */ | ||
| private static function _schedule(): void | ||
| { | ||
| $interval = 0; | ||
| foreach(self::$_connections as $id => $reference) | ||
| { | ||
| $connection = $reference->get(); | ||
| if($connection === null) | ||
| { | ||
| unset(self::$_connections[$id], self::$_failed[$id]); | ||
| continue; | ||
| } | ||
| $connectionInterval = self::_interval($connection); | ||
| if($interval === 0 || $connectionInterval < $interval) | ||
| { | ||
| $interval = $connectionInterval; | ||
| } | ||
| } | ||
|
|
||
| if($interval === 0) | ||
| { | ||
| self::_cancel(); | ||
| return; | ||
| } | ||
|
|
||
| if(!self::$_handlerInstalled) | ||
| { | ||
| pcntl_async_signals(true); | ||
| pcntl_signal(SIGALRM, [self::class, 'beat'], true); | ||
| self::$_handlerInstalled = true; | ||
| } | ||
|
|
||
| $due = time() + $interval; | ||
| if(self::$_nextBeat === 0 || $due < self::$_nextBeat) | ||
| { | ||
| pcntl_alarm($interval); | ||
| self::$_nextBeat = $due; | ||
| } | ||
| } | ||
|
|
||
| private static function _cancel(): void | ||
| { | ||
| if(self::$_handlerInstalled) | ||
| { | ||
| pcntl_alarm(0); | ||
| pcntl_signal(SIGALRM, SIG_IGN); | ||
| self::$_handlerInstalled = false; | ||
| } | ||
| self::$_nextBeat = 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.