-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscriptionManager.php
More file actions
130 lines (119 loc) · 4 KB
/
SubscriptionManager.php
File metadata and controls
130 lines (119 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace Arcp\Runtime;
use Arcp\Envelope\Envelope;
use Arcp\Envelope\Priority;
use Arcp\Errors\InvalidArgumentException;
use Arcp\Ids\SubscriptionId;
use Arcp\Json\EnvelopeSerializer;
use Arcp\Messages\Subscriptions\Subscribe;
/**
* Holds active subscriptions and dispatches matching envelopes through
* `subscribe.event` wrappers (RFC §13). Backfill replays from the event
* log up to the current write head, then concludes with a synthetic
* `subscription.backfill_complete` event before live tail begins.
*/
final class SubscriptionManager
{
/** @var array<string, Subscription> */
private array $byId = [];
public function __construct(private readonly EnvelopeSerializer $serializer)
{
}
public function compile(Session $session, Subscribe $msg): Subscription
{
$sessionIds = $this->extractStringList($msg->filter, 'session_id');
$traceIds = $this->extractStringList($msg->filter, 'trace_id');
$jobIds = $this->extractStringList($msg->filter, 'job_id');
$streamIds = $this->extractStringList($msg->filter, 'stream_id');
$types = $this->extractStringList($msg->filter, 'types');
$min = Priority::Low;
if (isset($msg->filter['min_priority']) && \is_string($msg->filter['min_priority'])) {
$min = Priority::tryFrom($msg->filter['min_priority']) ?? throw new InvalidArgumentException(
'min_priority not recognized',
['min_priority' => $msg->filter['min_priority']],
);
}
$sub = new Subscription(
SubscriptionId::random(),
$session,
$sessionIds,
$traceIds,
$jobIds,
$streamIds,
$types,
$min,
);
$this->byId[(string) $sub->id] = $sub;
return $sub;
}
public function close(SubscriptionId $id): bool
{
$key = (string) $id;
if (!isset($this->byId[$key])) {
return false;
}
unset($this->byId[$key]);
return true;
}
public function get(SubscriptionId $id): ?Subscription
{
return $this->byId[(string) $id] ?? null;
}
public function dispatch(Envelope $env): void
{
foreach ($this->byId as $sub) {
if (!$sub->matches($env)) {
continue;
}
$wrapper = new \Arcp\Messages\Subscriptions\SubscribeEvent(
$this->serializer->envelopeToArray($env),
);
$session = $sub->session;
// Direct send: bypass dispatcher because subscriptions are pure
// delivery from the runtime's perspective (RFC §13).
try {
$session->transport->send(new Envelope(
id: \Arcp\Ids\MessageId::random(),
payload: $wrapper,
timestamp: new \DateTimeImmutable('now', new \DateTimeZone('UTC')),
sessionId: $session->sessionId,
subscriptionId: $sub->id,
priority: $env->priority,
));
} catch (\Throwable) {
$this->close($sub->id);
}
}
}
/**
* @param array<string, mixed> $filter
*
* @return list<string>
*/
private function extractStringList(array $filter, string $key): array
{
if (!isset($filter[$key])) {
return [];
}
$val = $filter[$key];
if (\is_string($val)) {
return [$val];
}
if (!\is_array($val)) {
throw new InvalidArgumentException("filter.$key must be string or list of strings");
}
$out = [];
foreach ($val as $entry) {
if (!\is_string($entry)) {
throw new InvalidArgumentException("filter.$key entries must be strings");
}
$out[] = $entry;
}
return $out;
}
public function count(): int
{
return \count($this->byId);
}
}