Lines
92.72%
102 / 110
Methods
72.22%
13 / 18
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| push | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| pushBurst | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| emit | 83.33% | 5 / 6 | 0.00% | 0 / 1 | 3.04 | ||
| startDrainingIfPending | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 3 | ||
| discardPendingWork | 87.50% | 7 / 8 | 0.00% | 0 / 1 | 3.02 | ||
| recordHandlerCall | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 2.03 | ||
| cancel | 72.72% | 8 / 11 | 0.00% | 0 / 1 | 4.32 | ||
| pushWithin | 81.81% | 9 / 11 | 0.00% | 0 / 1 | 4.10 | ||
| dropOnOverflow | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | ||
| findOldestSupersededIndex | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 6 | ||
| dropSupersededStateChange | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 4 | ||
| dropOldestEvent | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | ||
| recordOverflowDrop | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| enqueue | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| startDraining | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| drain | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 5 | ||
| runQueuedWork | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | ||
| 16 | final class SubscriptionQueue | |
| 17 | { | |
| 18 | /** @var SplQueue<QueuedWork> */ | |
| 19 | private SplQueue $pending; | |
| 20 | ||
| 21 | private int $pendingEvents = 0; | |
| 22 | ||
| 23 | private bool $draining = false; | |
| 24 | ||
| 25 | private bool $cancelled = false; | |
| 26 | ||
| 27 | public readonly SubscriptionActivity $activity; | |
| 28 | ||
| 29 | public function __construct( | |
| 30 | public readonly RegisteredSubscription $subscription, | |
| 31 | private readonly int $limit, | |
| 32 | private readonly DispatchListener $listener, | |
| 33 | private readonly ScopeState $scopeState, | |
| 34 | private readonly Clock $clock, | |
| 35 | ) { | |
| 36 | $this->pending = new SplQueue(); | |
| 37 | $this->activity = new SubscriptionActivity($subscription); | |
| 38 | } | |
| 39 | ||
| 40 | /** @param ?Closure(QueueDiscardReason): void $onDiscarded */ | |
| 41 | public function push(object $event, ?Closure $onDiscarded = null): void | |
| 42 | { | |
| 43 | $this->pushWithin($event, $this->limit, $onDiscarded); | |
| 44 | } | |
| 45 | ||
| 46 | /** @param list<StateChange> $changes */ | |
| 47 | public function pushBurst(array $changes): void | |
| 48 | { | |
| 49 | $capacity = max($this->limit, \count($changes)); | |
| 50 | ||
| 51 | foreach ($changes as $change) { | |
| 52 | $this->pushWithin($change, $capacity); | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | /** @param Closure(): void $emission */ | |
| 57 | public function emit(Closure $emission): void | |
| 58 | { | |
| 59 | if ($this->cancelled) { | |
| 60 | return; | |
| 61 | } | |
| 62 | ||
| 63 | if ($this->scopeState->isPaused()) { | |
| 64 | $this->listener->eventSuppressed($this->subscription, null); | |
| 65 | ||
| 66 | return; | |
| 67 | } | |
| 68 | ||
| 69 | $this->enqueue(QueuedWork::forStreamEmission($emission)); | |
| 70 | } | |
| 71 | ||
| 72 | public function startDrainingIfPending(): void | |
| 73 | { | |
| 74 | if (!$this->cancelled && !$this->pending->isEmpty()) { | |
| 75 | $this->startDraining(); | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | public function discardPendingWork(): void | |
| 80 | { | |
| 81 | if ($this->cancelled) { | |
| 82 | return; | |
| 83 | } | |
| 84 | ||
| 85 | $discarded = $this->pending; | |
| 86 | $this->pending = new SplQueue(); | |
| 87 | $this->pendingEvents = 0; | |
| 88 | ||
| 89 | foreach ($discarded as $work) { | |
| 90 | $this->listener->eventSuppressed($this->subscription, $work->event); | |
| 91 | $work->discard(QueueDiscardReason::Paused); | |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | public function recordHandlerCall(): void | |
| 96 | { | |
| 97 | if ($this->cancelled) { | |
| 98 | return; | |
| 99 | } | |
| 100 | ||
| 101 | $calledAt = $this->clock->getNow(); | |
| 102 | $this->activity->recordHandled($calledAt); | |
| 103 | $this->listener->handlerCalled($this->subscription, $calledAt); | |
| 104 | } | |
| 105 | ||
| 106 | public function cancel(): void | |
| 107 | { | |
| 108 | if ($this->cancelled) { | |
| 109 | return; | |
| 110 | } | |
| 111 | ||
| 112 | $this->cancelled = true; | |
| 113 | $discarded = $this->pending; | |
| 114 | $this->pending = new SplQueue(); | |
| 115 | $this->pendingEvents = 0; | |
| 116 | ||
| 117 | foreach ($discarded as $work) { | |
| 118 | $work->discard(QueueDiscardReason::Cancelled); | |
| 119 | } | |
| 120 | ||
| 121 | try { | |
| 122 | $this->subscription->subscriptionScope->close(); | |
| 123 | } catch (Throwable $e) { | |
| 124 | $this->listener->handlerFailed($this->subscription, $e); | |
| 125 | } | |
| 126 | } | |
| 127 | ||
| 128 | /** @param ?Closure(QueueDiscardReason): void $onDiscarded */ | |
| 129 | private function pushWithin(object $event, int $capacity, ?Closure $onDiscarded = null): void | |
| 130 | { | |
| 131 | if ($this->cancelled) { | |
| 132 | $onDiscarded?->__invoke(QueueDiscardReason::Cancelled); | |
| 133 | ||
| 134 | return; | |
| 135 | } | |
| 136 | ||
| 137 | if ($this->scopeState->isPaused()) { | |
| 138 | $this->listener->eventSuppressed($this->subscription, $event); | |
| 139 | $onDiscarded?->__invoke(QueueDiscardReason::Paused); | |
| 140 | ||
| 141 | return; | |
| 142 | } | |
| 143 | ||
| 144 | if ($this->pendingEvents >= $capacity) { | |
| 145 | $event = $this->dropOnOverflow($event); | |
| 146 | } | |
| 147 | ||
| 148 | ++$this->pendingEvents; | |
| 149 | ||
| 150 | $this->enqueue(QueuedWork::forDroppableEvent($event, $this->subscription->handler, $onDiscarded)); | |
| 151 | } | |
| 152 | ||
| 153 | private function dropOnOverflow(object $incoming): object | |
| 154 | { | |
| 155 | if ($incoming instanceof StateChange) { | |
| 156 | $superseded = $this->findOldestSupersededIndex($incoming); | |
| 157 | ||
| 158 | if ($superseded !== null) { | |
| 159 | return $this->dropSupersededStateChange($superseded, $incoming); | |
| 160 | } | |
| 161 | } | |
| 162 | ||
| 163 | $this->dropOldestEvent(); | |
| 164 | ||
| 165 | return $incoming; | |
| 166 | } | |
| 167 | ||
| 168 | private function findOldestSupersededIndex(StateChange $incoming): ?int | |
| 169 | { | |
| 170 | $firstIndexByEntity = []; | |
| 171 | $supersededIndexes = []; | |
| 172 | ||
| 173 | foreach ($this->pending as $index => $work) { | |
| 174 | if (!$work->event instanceof StateChange) { | |
| 175 | continue; | |
| 176 | } | |
| 177 | ||
| 178 | $entityId = $work->event->entityId->value; | |
| 179 | ||
| 180 | if (isset($firstIndexByEntity[$entityId])) { | |
| 181 | $supersededIndexes[] = $firstIndexByEntity[$entityId]; | |
| 182 | ||
| 183 | continue; | |
| 184 | } | |
| 185 | ||
| 186 | $firstIndexByEntity[$entityId] = $index; | |
| 187 | } | |
| 188 | ||
| 189 | if (isset($firstIndexByEntity[$incoming->entityId->value])) { | |
| 190 | $supersededIndexes[] = $firstIndexByEntity[$incoming->entityId->value]; | |
| 191 | } | |
| 192 | ||
| 193 | return $supersededIndexes === [] ? null : min($supersededIndexes); | |
| 194 | } | |
| 195 | ||
| 196 | private function dropSupersededStateChange(int $index, StateChange $incoming): StateChange | |
| 197 | { | |
| 198 | $superseded = $this->pending->offsetGet($index); | |
| 199 | \assert($superseded->event instanceof StateChange); | |
| 200 | ||
| 201 | $this->pending->offsetUnset($index); | |
| 202 | $this->recordOverflowDrop($superseded); | |
| 203 | ||
| 204 | $entityId = $superseded->event->entityId->value; | |
| 205 | $from = $superseded->event->from; | |
| 206 | ||
| 207 | for ($next = $index; $next < $this->pending->count(); ++$next) { | |
| 208 | $work = $this->pending->offsetGet($next); | |
| 209 | ||
| 210 | if ($work->event instanceof StateChange && $work->event->entityId->value === $entityId) { | |
| 211 | $this->pending->offsetSet($next, $work->withEvent($work->event->withFrom($from))); | |
| 212 | ||
| 213 | return $incoming; | |
| 214 | } | |
| 215 | } | |
| 216 | ||
| 217 | return $incoming->withFrom($from); | |
| 218 | } | |
| 219 | ||
| 220 | private function dropOldestEvent(): void | |
| 221 | { | |
| 222 | foreach ($this->pending as $index => $work) { | |
| 223 | if (!$work->isDroppable()) { | |
| 224 | continue; | |
| 225 | } | |
| 226 | ||
| 227 | $this->pending->offsetUnset($index); | |
| 228 | $this->recordOverflowDrop($work); | |
| 229 | ||
| 230 | return; | |
| 231 | } | |
| 232 | } | |
| 233 | ||
| 234 | private function recordOverflowDrop(QueuedWork $work): void | |
| 235 | { | |
| 236 | --$this->pendingEvents; | |
| 237 | $this->activity->recordDropped(); | |
| 238 | ||
| 239 | $this->listener->eventDropped($this->subscription, $this->activity->dropped); | |
| 240 | $work->discard(QueueDiscardReason::Overflow); | |
| 241 | } | |
| 242 | ||
| 243 | private function enqueue(QueuedWork $work): void | |
| 244 | { | |
| 245 | $this->pending->enqueue($work); | |
| 246 | ||
| 247 | if ($this->scopeState->isLive()) { | |
| 248 | $this->startDraining(); | |
| 249 | } | |
| 250 | } | |
| 251 | ||
| 252 | private function startDraining(): void | |
| 253 | { | |
| 254 | if ($this->draining) { | |
| 255 | return; | |
| 256 | } | |
| 257 | ||
| 258 | $this->draining = true; | |
| 259 | ||
| 260 | async($this->drain(...))->ignore(); | |
| 261 | } | |
| 262 | ||
| 263 | private function drain(): void | |
| 264 | { | |
| 265 | try { | |
| 266 | while (!$this->cancelled && !$this->scopeState->isPaused() && !$this->pending->isEmpty()) { | |
| 267 | $work = $this->pending->dequeue(); | |
| 268 | ||
| 269 | if ($work->isDroppable()) { | |
| 270 | --$this->pendingEvents; | |
| 271 | } | |
| 272 | ||
| 273 | $this->runQueuedWork($work); | |
| 274 | } | |
| 275 | } finally { | |
| 276 | $this->draining = false; | |
| 277 | } | |
| 278 | } | |
| 279 | ||
| 280 | private function runQueuedWork(QueuedWork $work): void | |
| 281 | { | |
| 282 | if ($work->event !== null) { | |
| 283 | $this->activity->recordReceived($this->clock->getNow()); | |
| 284 | $this->listener->eventReceived($this->subscription, $work->event); | |
| 285 | } | |
| 286 | ||
| 287 | try { | |
| 288 | $work(); | |
| 289 | } catch (Throwable $e) { | |
| 290 | $this->listener->handlerFailed($this->subscription, $e); | |
| 291 | ||
| 292 | return; | |
| 293 | } | |
| 294 | ||
| 295 | if ($work->isDroppable()) { | |
| 296 | $this->listener->eventDelivered($this->subscription); | |
| 297 | } | |
| 298 | } | |
| 299 | } |