Skip to main content

hyperlane_plugin_idempotency/
trait.rs

1use super::*;
2
3/// Pluggable asynchronous storage contract backing [`IdempotencyPlugin`].
4pub trait IdempotencyStore: Send + Sync + 'static {
5    /// Atomically checks an existing entry or acquires an exclusive `InFlight` lock for `key`.
6    ///
7    /// # Arguments
8    ///
9    /// - `&'a self` - Store reference.
10    /// - `&'a str` - Scoped idempotency storage key.
11    /// - `u64` - Request `(method, path, body)` `XxHash3_64` fingerprint.
12    /// - `u64` - Maximum `InFlight` lock TTL in milliseconds.
13    ///
14    /// # Returns
15    ///
16    /// - `IdempotencyFuture<'a, IdempotencyLookup>` - Acquisition, conflict, mismatch, or cached replay result.
17    fn acquire<'a>(
18        &'a self,
19        key: &'a str,
20        fingerprint: u64,
21        in_flight_ttl_ms: u64,
22    ) -> IdempotencyFuture<'a, IdempotencyLookup>;
23
24    /// Commits a completed response for `key` with the given `ttl_ms`.
25    ///
26    /// # Arguments
27    ///
28    /// - `&'a self` - Store reference.
29    /// - `String` - Scoped idempotency storage key.
30    /// - `u64` - Request `(method, path, body)` fingerprint.
31    /// - `Arc<StoredIdempotentResponse>` - Cached response and prebuilt replay wire buffer.
32    /// - `u64` - Replay cache TTL in milliseconds.
33    ///
34    /// # Returns
35    ///
36    /// - `IdempotencyFuture<'a, ()>` - Completion future.
37    fn complete<'a>(
38        &'a self,
39        key: String,
40        fingerprint: u64,
41        response: Arc<StoredIdempotentResponse>,
42        ttl_ms: u64,
43    ) -> IdempotencyFuture<'a, ()>;
44
45    /// Releases an `InFlight` entry without caching (for example on uncacheable `5xx` responses).
46    ///
47    /// # Arguments
48    ///
49    /// - `&'a self` - Store reference.
50    /// - `&'a str` - Scoped idempotency storage key.
51    ///
52    /// # Returns
53    ///
54    /// - `IdempotencyFuture<'a, ()>` - Completion future.
55    fn release<'a>(&'a self, key: &'a str) -> IdempotencyFuture<'a, ()>;
56
57    /// Synchronous best-effort cleanup hook invoked when [`IdempotencyInFlightGuard`] drops early.
58    ///
59    /// # Arguments
60    ///
61    /// - `&str` - Scoped idempotency storage key.
62    fn release_on_drop(&self, _key: &str) {}
63
64    /// Evicts expired entries and returns the count removed.
65    ///
66    /// # Returns
67    ///
68    /// - `usize` - Number of expired entries evicted.
69    fn purge_expired(&self) -> usize {
70        0
71    }
72}