Lines
98.01%
99 / 101
Functions and Methods
90.47%
19 / 21
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| LocalDispatcher | 98.01% | 99 / 101 | 90.47% | 19 / 21 | 48 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | |||||
| register | 100.00% | 34 / 34 | 100.00% | 1 / 1 | 9 | |||||
| startDrainingQueuesOf | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| discardPendingWorkOf | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| dispatchStateChange | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| dispatchStateChanges | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 4 | |||||
| dispatchEvent | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| dispatchTopic | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| dispatchMqttMessage | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| dispatchTrigger | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| dispatchConnection | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 3 | |||||
| dispatchExposedCommand | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 4 | |||||
| cancel | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | |||||
| cancelAll | 50.00% | 1 / 2 | 0.00% | 0 / 1 | 2.50 | |||||
| cancelSubscriptionsOf | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| countFor | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| listActivitiesOf | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| refusedSubscription | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | |||||
| deliver | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 4 | |||||
| listStateChangeQueuesMatching | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 4 | |||||
| listQueuesOf | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Dispatch; | |
| 6 | ||
| 7 | use Closure; | |
| 8 | use LogicException; | |
| 9 | use Stewart\Contracts\Connection\ConnectionEvent; | |
| 10 | use Stewart\Contracts\Event\HaEvent; | |
| 11 | use Stewart\Contracts\Exposure\Command\ExposedCommand; | |
| 12 | use Stewart\Contracts\Exposure\ExposedEntityKey; | |
| 13 | use Stewart\Contracts\Mqtt\MqttMessage; | |
| 14 | use Stewart\Contracts\Registry\EntityFilter; | |
| 15 | use Stewart\Contracts\Selector\Selector; | |
| 16 | use Stewart\Contracts\State\StateChange; | |
| 17 | use Stewart\Contracts\Subscription; | |
| 18 | use Stewart\Contracts\Time\Clock; | |
| 19 | use Stewart\Contracts\Topic\TopicEvent; | |
| 20 | use Stewart\Contracts\Trigger\TriggerEvent; | |
| 21 | use Stewart\Runtime\Ipc\WorkerSettings; | |
| 22 | use Stewart\Runtime\Model\Collection\SubscriptionIdCollection; | |
| 23 | use Stewart\Runtime\Model\ResourceScope; | |
| 24 | use Stewart\Runtime\Model\SubscriptionId; | |
| 25 | use Stewart\Runtime\Model\SubscriptionKind; | |
| 26 | use Stewart\Runtime\Model\WorkerId; | |
| 27 | use Stewart\Runtime\Registry\RegistryCache; | |
| 28 | use Stewart\Runtime\Scope\ScopeLifecycle; | |
| 29 | use Stewart\Support\State\Collection\StateChangeCollection; | |
| 30 | use Stewart\Support\Stream\SubscriptionScope; | |
| 31 | use Stewart\Support\Wire\TriggerSpec; | |
| 32 | use Throwable; | |
| 33 | ||
| 34 | final class LocalDispatcher | |
| 35 | { | |
| 36 | /** @var array<string, SubscriptionQueue> */ | |
| 37 | private array $queues = []; | |
| 38 | ||
| 39 | private int $counter = 0; | |
| 40 | ||
| 41 | private readonly string $subscriptionIdPrefix; | |
| 42 | ||
| 43 | private readonly int $subscriptionQueueLimit; | |
| 44 | ||
| 45 | private readonly SelectorIndex $stateIndex; | |
| 46 | ||
| 47 | private readonly EntityFilterIndex $stateFilterIndex; | |
| 48 | ||
| 49 | public function __construct( | |
| 50 | WorkerId $workerId, | |
| 51 | WorkerSettings $workerSettings, | |
| 52 | private readonly DispatchListener $listener, | |
| 53 | private readonly SubscriptionListener $subscriptions, | |
| 54 | private readonly ScopeLifecycle $scopes, | |
| 55 | RegistryCache $registry, | |
| 56 | private readonly Clock $clock, | |
| 57 | ) { | |
| 58 | $this->subscriptionIdPrefix = $workerId->toProcessIdPrefix(); | |
| 59 | $this->subscriptionQueueLimit = $workerSettings->subscriptionBuffer; | |
| 60 | $this->stateIndex = new SelectorIndex(); | |
| 61 | $this->stateFilterIndex = new EntityFilterIndex($registry); | |
| 62 | } | |
| 63 | ||
| 64 | public function register( | |
| 65 | ResourceScope $scope, | |
| 66 | SubscriptionKind $kind, | |
| 67 | Selector $selector, | |
| 68 | SubscriptionScope $subscriptionScope, | |
| 69 | Closure $handler, | |
| 70 | ?TriggerSpec $trigger = null, | |
| 71 | ?EntityFilter $entityFilter = null, | |
| 72 | ): Subscription { | |
| 73 | if ($kind->needsTriggerSpec() !== ($trigger !== null)) { | |
| 74 | throw new LogicException(\sprintf('A %s subscription %s a trigger spec.', $kind->value, $trigger === null ? 'needs' : 'takes no')); | |
| 75 | } | |
| 76 | ||
| 77 | if ($entityFilter !== null && $kind !== SubscriptionKind::StateChange) { | |
| 78 | throw new LogicException(\sprintf('A %s subscription takes no entity filter.', $kind->value)); | |
| 79 | } | |
| 80 | ||
| 81 | $id = SubscriptionId::fromString($this->subscriptionIdPrefix . ':' . $this->counter++); | |
| 82 | ||
| 83 | $scopeState = $this->scopes->findOrCreateStateOf($scope); | |
| 84 | ||
| 85 | if ($scopeState->isClosed()) { | |
| 86 | return $this->refusedSubscription($id, $subscriptionScope); | |
| 87 | } | |
| 88 | ||
| 89 | $subscription = new RegisteredSubscription( | |
| 90 | $id, | |
| 91 | $scope, | |
| 92 | $kind, | |
| 93 | $selector, | |
| 94 | $subscriptionScope, | |
| 95 | $handler, | |
| 96 | $trigger, | |
| 97 | $entityFilter, | |
| 98 | ); | |
| 99 | ||
| 100 | $queue = new SubscriptionQueue($subscription, $this->subscriptionQueueLimit, $this->listener, $scopeState, $this->clock); | |
| 101 | ||
| 102 | $this->queues[$subscription->id->value] = $queue; | |
| 103 | $subscriptionScope->deliverVia($queue->emit(...)); | |
| 104 | $subscriptionScope->reportHandlerCallsTo($queue->recordHandlerCall(...)); | |
| 105 | ||
| 106 | if ($entityFilter !== null) { | |
| 107 | $this->stateFilterIndex->add($subscription->id, $entityFilter); | |
| 108 | } elseif ($kind === SubscriptionKind::StateChange) { | |
| 109 | $this->stateIndex->add($subscription->id, $kind, $selector); | |
| 110 | } | |
| 111 | ||
| 112 | try { | |
| 113 | $this->subscriptions->subscriptionRegistered($subscription); | |
| 114 | } catch (Throwable $e) { | |
| 115 | unset($this->queues[$subscription->id->value]); | |
| 116 | $this->stateIndex->remove($subscription->id); | |
| 117 | $this->stateFilterIndex->remove($subscription->id); | |
| 118 | $queue->cancel(); | |
| 119 | ||
| 120 | throw $e; | |
| 121 | } | |
| 122 | ||
| 123 | return new LocalSubscription($subscription->id, $this->cancel(...)); | |
| 124 | } | |
| 125 | ||
| 126 | public function startDrainingQueuesOf(ResourceScope $scope): void | |
| 127 | { | |
| 128 | foreach ($this->listQueuesOf($scope) as $queue) { | |
| 129 | $queue->startDrainingIfPending(); | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | public function discardPendingWorkOf(ResourceScope $scope): void | |
| 134 | { | |
| 135 | foreach ($this->listQueuesOf($scope) as $queue) { | |
| 136 | $queue->discardPendingWork(); | |
| 137 | } | |
| 138 | } | |
| 139 | ||
| 140 | public function dispatchStateChange(StateChange $change): void | |
| 141 | { | |
| 142 | $this->dispatchStateChanges(StateChangeCollection::fromList([$change])); | |
| 143 | } | |
| 144 | ||
| 145 | public function dispatchStateChanges(StateChangeCollection $changes): void | |
| 146 | { | |
| 147 | $changesByQueue = []; | |
| 148 | ||
| 149 | foreach ($changes as $change) { | |
| 150 | foreach ($this->listStateChangeQueuesMatching($change) as $subscriptionId => $queue) { | |
| 151 | $changesByQueue[$subscriptionId][] = $change; | |
| 152 | } | |
| 153 | } | |
| 154 | ||
| 155 | foreach ($changesByQueue as $subscriptionId => $queueChanges) { | |
| 156 | $this->queues[$subscriptionId]->pushBurst($queueChanges); | |
| 157 | } | |
| 158 | } | |
| 159 | ||
| 160 | public function dispatchEvent(HaEvent $event, SubscriptionIdCollection $deliverTo): void | |
| 161 | { | |
| 162 | $this->deliver(SubscriptionKind::Event, $event, $deliverTo); | |
| 163 | } | |
| 164 | ||
| 165 | public function dispatchTopic(TopicEvent $event, SubscriptionIdCollection $deliverTo): void | |
| 166 | { | |
| 167 | $this->deliver(SubscriptionKind::Topic, $event, $deliverTo); | |
| 168 | } | |
| 169 | ||
| 170 | public function dispatchMqttMessage(MqttMessage $message, SubscriptionIdCollection $deliverTo): void | |
| 171 | { | |
| 172 | $this->deliver(SubscriptionKind::Mqtt, $message, $deliverTo); | |
| 173 | } | |
| 174 | ||
| 175 | public function dispatchTrigger(TriggerEvent $event, SubscriptionIdCollection $deliverTo): void | |
| 176 | { | |
| 177 | $this->deliver(SubscriptionKind::Trigger, $event, $deliverTo); | |
| 178 | } | |
| 179 | ||
| 180 | public function dispatchConnection(ConnectionEvent $event): void | |
| 181 | { | |
| 182 | foreach ($this->queues as $queue) { | |
| 183 | if ($queue->subscription->kind === SubscriptionKind::Connection) { | |
| 184 | $queue->push($event); | |
| 185 | } | |
| 186 | } | |
| 187 | } | |
| 188 | ||
| 189 | /** @param Closure(QueueDiscardReason): void $onDiscarded */ | |
| 190 | public function dispatchExposedCommand(ResourceScope $scope, ExposedEntityKey $key, ExposedCommand $command, Closure $onDiscarded): bool | |
| 191 | { | |
| 192 | $queues = array_filter( | |
| 193 | $this->listQueuesOf($scope), | |
| 194 | static fn(SubscriptionQueue $queue): bool => $queue->subscription->kind === SubscriptionKind::ExposedCommand && $queue->subscription->selector->matches($key->value), | |
| 195 | ); | |
| 196 | $undiscarded = \count($queues); | |
| 197 | ||
| 198 | // Another subscription may still handle the command, so only the last discard reports it. | |
| 199 | $discardOne = static function (QueueDiscardReason $reason) use (&$undiscarded, $onDiscarded): void { | |
| 200 | if (--$undiscarded === 0) { | |
| 201 | $onDiscarded($reason); | |
| 202 | } | |
| 203 | }; | |
| 204 | ||
| 205 | foreach ($queues as $queue) { | |
| 206 | $queue->push($command, $discardOne); | |
| 207 | } | |
| 208 | ||
| 209 | return $queues !== []; | |
| 210 | } | |
| 211 | ||
| 212 | public function cancel(SubscriptionId $subscriptionId): void | |
| 213 | { | |
| 214 | $queue = $this->queues[$subscriptionId->value] ?? null; | |
| 215 | ||
| 216 | if ($queue === null) { | |
| 217 | return; | |
| 218 | } | |
| 219 | ||
| 220 | unset($this->queues[$subscriptionId->value]); | |
| 221 | $this->stateIndex->remove($subscriptionId); | |
| 222 | $this->stateFilterIndex->remove($subscriptionId); | |
| 223 | $queue->cancel(); | |
| 224 | ||
| 225 | $this->subscriptions->subscriptionCancelled($queue->subscription); | |
| 226 | } | |
| 227 | ||
| 228 | public function cancelAll(): void | |
| 229 | { | |
| 230 | foreach ($this->queues as $queue) { | |
| 231 | $this->cancel($queue->subscription->id); | |
| 232 | } | |
| 233 | } | |
| 234 | ||
| 235 | public function cancelSubscriptionsOf(ResourceScope $scope): void | |
| 236 | { | |
| 237 | foreach ($this->listQueuesOf($scope) as $queue) { | |
| 238 | $this->cancel($queue->subscription->id); | |
| 239 | } | |
| 240 | } | |
| 241 | ||
| 242 | public function countFor(ResourceScope $scope): int | |
| 243 | { | |
| 244 | return \count($this->listQueuesOf($scope)); | |
| 245 | } | |
| 246 | ||
| 247 | /** @return list<SubscriptionActivity> */ | |
| 248 | public function listActivitiesOf(ResourceScope $scope): array | |
| 249 | { | |
| 250 | return array_values(array_map(static fn(SubscriptionQueue $queue): SubscriptionActivity => $queue->activity, $this->listQueuesOf($scope))); | |
| 251 | } | |
| 252 | ||
| 253 | private function refusedSubscription(SubscriptionId $id, SubscriptionScope $subscriptionScope): Subscription | |
| 254 | { | |
| 255 | $subscriptionScope->close(); | |
| 256 | ||
| 257 | $subscription = new LocalSubscription($id, static function (): void {}); | |
| 258 | $subscription->unsubscribe(); | |
| 259 | ||
| 260 | return $subscription; | |
| 261 | } | |
| 262 | ||
| 263 | private function deliver(SubscriptionKind $kind, object $event, SubscriptionIdCollection $deliverTo): void | |
| 264 | { | |
| 265 | foreach ($deliverTo as $id) { | |
| 266 | $queue = $this->queues[$id->value] ?? null; | |
| 267 | ||
| 268 | if ($queue !== null && $queue->subscription->kind === $kind) { | |
| 269 | $queue->push($event); | |
| 270 | } | |
| 271 | } | |
| 272 | } | |
| 273 | ||
| 274 | /** @return array<string, SubscriptionQueue> */ | |
| 275 | private function listStateChangeQueuesMatching(StateChange $change): array | |
| 276 | { | |
| 277 | $matching = []; | |
| 278 | $deliverTo = [ | |
| 279 | ...$this->stateIndex->findMatching(SubscriptionKind::StateChange, $change->entityId->value), | |
| 280 | ...$this->stateFilterIndex->findMatching($change->entityId), | |
| 281 | ]; | |
| 282 | ||
| 283 | foreach ($deliverTo as $id) { | |
| 284 | $queue = $this->queues[$id->value] ?? null; | |
| 285 | ||
| 286 | if ($queue !== null && $queue->subscription->kind === SubscriptionKind::StateChange) { | |
| 287 | $matching[$id->value] = $queue; | |
| 288 | } | |
| 289 | } | |
| 290 | ||
| 291 | return $matching; | |
| 292 | } | |
| 293 | ||
| 294 | /** @return array<string, SubscriptionQueue> */ | |
| 295 | private function listQueuesOf(ResourceScope $scope): array | |
| 296 | { | |
| 297 | return array_filter($this->queues, static fn(SubscriptionQueue $queue): bool => $queue->subscription->scope->equals($scope)); | |
| 298 | } | |
| 299 | } |