Package at.aimon.session.web.spi
Interface IdempotencyStore
- All Known Implementing Classes:
InMemoryIdempotencyStore
public interface IdempotencyStore
Idempotency store SPI per design ยง9.2.
Tracks IdempotencyEntrys keyed by client-supplied idempotency key. Provides atomic
putIfAbsent for first-arrival detection,
markDone for caching the final result, and a small set of operations
needed by the holder-loss sweeper (touch(java.lang.String, java.lang.String), compareAndReset(java.lang.String, java.lang.String), findStaleInFlight(java.time.Instant)).
-
Method Summary
Modifier and TypeMethodDescriptionbooleancompareAndReset(String key, String expectedHolderId) Atomically reset (or transition to FAILED) a stale in-flight entry held byexpectedHolderId.Look up an entry by key.findStaleInFlight(Instant cutoff) Enumerate in-flight entries whoselastTouchedAtis older thancutoff.voidTransition an in-flight entry toIdempotencyEntry.Status.DONEand cache its result.putIfAbsent(String key, IdempotencyEntry entry, Duration ttl) Insertentryonly if no entry exists forkey.booleanRefresh the secondary TTL of an in-flight entry.
-
Method Details
-
putIfAbsent
Insertentryonly if no entry exists forkey. Atomic.For
IdempotencyEntry.Status.IN_FLIGHTentries,ttlis the secondary TTL (~lease length, ~30s); uponmarkDone(java.lang.String, at.aimon.core.agent.AgentExecutionResult), the implementation switches to the longer primary TTL (typically 24h).- Parameters:
key- the idempotency key (must not be null)entry- the candidate entry (must not be null)ttl- initial TTL (must not be null)- Returns:
PutResult.inserted()on success, orPutResult.existing(IdempotencyEntry)when an entry already exists
-
markDone
Transition an in-flight entry toIdempotencyEntry.Status.DONEand cache its result. Refreshes TTL to the primary value.- Parameters:
key- the idempotency key (must not be null)result- the final agent execution result (must not be null)
-
find
Look up an entry by key.- Parameters:
key- the idempotency key (must not be null)- Returns:
- the entry, or empty when none exists or it has expired
-
touch
Refresh the secondary TTL of an in-flight entry. Called from the lease renewer.- Parameters:
key- the idempotency key (must not be null)holderId- the calling holder; the touch is silently ignored when this does not match the entry's holder- Returns:
truewhen the touch was applied
-
compareAndReset
Atomically reset (or transition to FAILED) a stale in-flight entry held byexpectedHolderId. Two sweepers racing on the same stale entry must produce exactly one winner.- Parameters:
key- the idempotency key (must not be null)expectedHolderId- the holder id observed by the calling sweeper (must not be null)- Returns:
truewhen this caller won the race and reset the entry
-
findStaleInFlight
Enumerate in-flight entries whoselastTouchedAtis older thancutoff.Implementations may use SCAN + per-entry TTL inspection (Redis) or a simple map walk (in-memory). Used by the holder-loss sweeper.
- Parameters:
cutoff- entries withlastTouchedAt < cutoffare returned (must not be null)- Returns:
- matching entries (never null; may be empty)
-