Package com.tnsai.idempotency
Annotation Interface Idempotent
Marks a
Tool (or action method) as side-effecting and therefore
requiring idempotency protection on retry (issue #91).
Without this annotation, @Resilience(retry = ...) on a
side-effecting tool will happily call it 2× / 3× on transient failure —
sending 3 emails, creating 3 GitHub issues, charging the card 3 times.
Annotating the tool tells the framework to:
- Derive a stable key from the call (per
strategy()). - Check the
IdempotencyStorebefore invocation. - On hit, behave per
onCacheHit(). - On miss, run the tool, cache the result for
ttlSeconds(). - Propagate the key to upstream services that honor it
(HTTP
Idempotency-Keyheader onWEB_SERVICEactions).
Example
@Idempotent(strategy = KeyStrategy.HASH_INPUT, ttlSeconds = 86400)
public class SendEmailTool implements Tool {
@Override
public String execute(String query) { ... }
}
For tools whose natural key is a single input field rather than
a hash of everything, set strategy = EXPLICIT and override
Tool.idempotencyKeyFor(input).
- Since:
- 0.5.0 (issue #91)
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanWhether to cache failures as well as successes.What to do when a retry hits the cache.How to derive the key.longHow long the cached result is honored, in seconds.
-
Element Details
-
strategy
KeyStrategy strategyHow to derive the key. Default isKeyStrategy.HASH_INPUT.- Default:
HASH_INPUT
-
ttlSeconds
long ttlSecondsHow long the cached result is honored, in seconds. Default 1 hour. Set higher for slow-changing operations (a "create issue" call's deduplication window can sensibly be 24h+); lower for things where stale cached results would be misleading.- Default:
3600L
-
onCacheHit
RetryBehavior onCacheHitWhat to do when a retry hits the cache. DefaultRetryBehavior.RETURN_CACHED.- Default:
RETURN_CACHED
-
cacheFailures
boolean cacheFailuresWhether to cache failures as well as successes. Whentrue, a retry of a failed call is short-circuited (peronCacheHit()); whenfalse(default), failures fall through and the tool may re-attempt. Cache-failures-on for deterministic-failure operations where re-attempt is wasteful; leave off for transient-failure operations where the caller does want a retry.- Default:
false
-