-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIdempotencyStore.java
More file actions
71 lines (61 loc) · 2.3 KB
/
IdempotencyStore.java
File metadata and controls
71 lines (61 loc) · 2.3 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
package dev.arcp.runtime.idempotency;
import dev.arcp.core.auth.Principal;
import dev.arcp.core.ids.JobId;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.jspecify.annotations.Nullable;
/**
* §7.2 idempotency: a {@code (principal, idempotency_key)} pair maps to a stable {@link JobId}
* within a sliding TTL window. Submitting an identical triple returns the existing job id; a
* conflicting payload yields {@code DUPLICATE_KEY}.
*/
public final class IdempotencyStore {
public record Conflict(JobId existing) {}
private record Key(String principal, String idempotencyKey) {}
private record Entry(JobId jobId, int payloadHash, Instant insertedAt) {}
private final ConcurrentHashMap<Key, Entry> entries = new ConcurrentHashMap<>();
private final Duration ttl;
private final Clock clock;
public IdempotencyStore(Clock clock, Duration ttl) {
this.clock = Objects.requireNonNull(clock, "clock");
this.ttl = Objects.requireNonNull(ttl, "ttl");
}
/**
* Look up an existing job id, or claim {@code freshId}. Returns:
*
* <ul>
* <li>{@code null}: caller proceeds with {@code freshId}.
* <li>{@code Conflict(existing)}: the same key already produced a job (identical payload →
* reuse; different payload → caller raises {@code DUPLICATE_KEY}).
* </ul>
*/
public @Nullable Conflict claim(
Principal principal, String idempotencyKey, int payloadHash, JobId freshId) {
prune();
Key key = new Key(principal.id(), idempotencyKey);
Entry existing =
entries.compute(
key,
(k, prior) -> {
if (prior == null) {
return new Entry(freshId, payloadHash, clock.instant());
}
return prior;
});
if (existing.jobId.equals(freshId)) {
return null;
}
return new Conflict(existing.jobId);
}
public boolean matchesPayload(Principal principal, String idempotencyKey, int payloadHash) {
Entry e = entries.get(new Key(principal.id(), idempotencyKey));
return e != null && e.payloadHash == payloadHash;
}
private void prune() {
Instant now = clock.instant();
entries.values().removeIf(e -> e.insertedAt.plus(ttl).isBefore(now));
}
}