-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
37 lines (31 loc) · 915 Bytes
/
main.php
File metadata and controls
37 lines (31 loc) · 915 Bytes
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
<?php
declare(strict_types=1);
/**
* @return list<array{type: string, seq: int, payload: array<string, mixed>}>
*/
function produceEvents(int $count): array
{
$events = [];
for ($seq = 1; $seq <= $count; $seq++) {
$events[] = ['type' => 'job.progress', 'seq' => $seq, 'payload' => ['percent' => $seq * 10]];
}
return $events;
}
/**
* @param list<array{type: string, seq: int, payload: array<string, mixed>}> $events
*
* @return list<array{type: string, after: int}>
*/
function acknowledgeWithBackpressure(array $events, int $window): array
{
$acks = [];
foreach ($events as $event) {
if ($event['seq'] % $window === 0) {
$acks[] = ['type' => 'session.ack', 'after' => $event['seq']];
}
}
return $acks;
}
foreach (acknowledgeWithBackpressure(produceEvents(10), 3) as $ack) {
printf("%s after=%d\n", $ack['type'], $ack['after']);
}