-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
30 lines (23 loc) · 964 Bytes
/
Copy pathmain.php
File metadata and controls
30 lines (23 loc) · 964 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
<?php
declare(strict_types=1);
/**
* @param array<string, array{agent: string, job_id: string}> $records
*
* @return array{0: string, 1: array<string, array{agent: string, job_id: string}>}
*/
function submitIdempotent(array $records, string $principal, string $key, string $agent): array
{
$recordKey = $principal . ':' . $key;
if (isset($records[$recordKey])) {
if ($records[$recordKey]['agent'] !== $agent) {
throw new RuntimeException('DUPLICATE_KEY');
}
return [$records[$recordKey]['job_id'], $records];
}
$records[$recordKey] = ['agent' => $agent, 'job_id' => 'job_' . substr(hash('sha256', $recordKey), 0, 12)];
return [$records[$recordKey]['job_id'], $records];
}
$records = [];
[$first, $records] = submitIdempotent($records, 'user:demo', 'retry-42', 'planner@1.0.0');
[$second] = submitIdempotent($records, 'user:demo', 'retry-42', 'planner@1.0.0');
printf("%s %s\n", $first, $second);