pub trait LeaseStore:
Send
+ Sync
+ 'static {
// Required methods
fn grant<'a>(
&'a self,
tenant: &'a str,
task: &'a str,
ttl_ms: i64,
deadline_ms: i64,
) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>;
fn renew<'a>(
&'a self,
tenant: &'a str,
task: &'a str,
now_ms: i64,
) -> Pin<Box<dyn Future<Output = Result<bool, A2aError>> + Send + 'a>>;
fn deadline<'a>(
&'a self,
tenant: &'a str,
task: &'a str,
) -> Pin<Box<dyn Future<Output = Result<Option<i64>, A2aError>> + Send + 'a>>;
fn release<'a>(
&'a self,
tenant: &'a str,
task: &'a str,
) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>;
// Provided methods
fn is_process_local(&self) -> bool { ... }
fn lapsed<'a>(
&'a self,
tenant: &'a str,
task: &'a str,
) -> Pin<Box<dyn Future<Output = Result<Option<bool>, A2aError>> + Send + 'a>> { ... }
fn lapsed_many<'a>(
&'a self,
leases: &'a [(String, String)],
) -> Pin<Box<dyn Future<Output = Result<HashMap<(String, String), bool>, A2aError>> + Send + 'a>> { ... }
fn sweep(
&self,
_now_ms: i64,
_lapsed_for_ms: i64,
) -> Pin<Box<dyn Future<Output = Result<u64, A2aError>> + Send + '_>> { ... }
}Expand description
Where leases are kept: a time to live and a deadline per task, keyed by
tenant ("" without one) and task id.
§Contract
grantsets the task’s time to live and its deadline, replacing any lease it had.renewmoves an existing lease’s deadline tonow_msplus its time to live and says whether there was a lease; it never creates one.deadlineis the current deadline,Nonewithout a lease.lapsedsays whether the deadline has passed,Nonewithout a lease. The handler asks this, notdeadline, when it decides to stop a task.releasedeletes the lease.- An error is an error: a store that cannot be read must not answer with a deadline in the past, which would stop a child whose parent is alive.
§Whose clock
deadline_ms and now_ms are read from the clock of the replica that
calls. That is exact for one process and approximate for several: a
renewal from a replica whose clock is behind the granting one shortens the
lease by the difference, and a difference as large as the lease stops the
child. A store whose backing system keeps a clock every replica shares may
ignore both arguments and use that clock for the grant, the renewal and
lapsed, so one clock writes the deadline and judges it:
PostgresLeaseStore (the postgres feature) does. Such a store’s
deadline is a time on its clock, which a caller must
not compare with its own; that is what lapsed is for.
Required Methods§
Sourcefn grant<'a>(
&'a self,
tenant: &'a str,
task: &'a str,
ttl_ms: i64,
deadline_ms: i64,
) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>
fn grant<'a>( &'a self, tenant: &'a str, task: &'a str, ttl_ms: i64, deadline_ms: i64, ) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>
Sourcefn renew<'a>(
&'a self,
tenant: &'a str,
task: &'a str,
now_ms: i64,
) -> Pin<Box<dyn Future<Output = Result<bool, A2aError>> + Send + 'a>>
fn renew<'a>( &'a self, tenant: &'a str, task: &'a str, now_ms: i64, ) -> Pin<Box<dyn Future<Output = Result<bool, A2aError>> + Send + 'a>>
Provided Methods§
Sourcefn is_process_local(&self) -> bool
fn is_process_local(&self) -> bool
Whether this store keeps its state in this process alone.
The default is false: a store this crate cannot see into (yours, over
Redis, say) is taken to be shared. The in-memory ones answer true.
Profile::Production reads it:
on a task store that is shared between processes, a store that only this
process can see makes the fleet behave as if it had no store
(audit E-F13).
Sourcefn lapsed<'a>(
&'a self,
tenant: &'a str,
task: &'a str,
) -> Pin<Box<dyn Future<Output = Result<Option<bool>, A2aError>> + Send + 'a>>
fn lapsed<'a>( &'a self, tenant: &'a str, task: &'a str, ) -> Pin<Box<dyn Future<Output = Result<Option<bool>, A2aError>> + Send + 'a>>
Whether task’s lease has lapsed: None without a lease.
The default compares deadline with this process’s
clock (now_unix_ms), which is right for a store that takes its
times from its callers. A store that keeps time itself overrides it,
so that the clock that wrote the deadline is the one that judges it.
§Errors
When the store cannot be read.
Sourcefn lapsed_many<'a>(
&'a self,
leases: &'a [(String, String)],
) -> Pin<Box<dyn Future<Output = Result<HashMap<(String, String), bool>, A2aError>> + Send + 'a>>
fn lapsed_many<'a>( &'a self, leases: &'a [(String, String)], ) -> Pin<Box<dyn Future<Output = Result<HashMap<(String, String), bool>, A2aError>> + Send + 'a>>
Whether each of leases (tenant, task) has lapsed, in one call: the
map holds an entry for each that has a lease, and none for one that
has none (released).
A handler’s watcher asks this once per check interval for every lease
it watches, so N leased turns cost the store one round trip per
interval instead of N (audit H5, ADR 0044). The default asks
lapsed for each, which is right for a store in
memory; a store behind a network overrides it with one statement, as
PostgresLeaseStore does.
§Errors
When the store cannot be read.
Sourcefn sweep(
&self,
_now_ms: i64,
_lapsed_for_ms: i64,
) -> Pin<Box<dyn Future<Output = Result<u64, A2aError>> + Send + '_>>
fn sweep( &self, _now_ms: i64, _lapsed_for_ms: i64, ) -> Pin<Box<dyn Future<Output = Result<u64, A2aError>> + Send + '_>>
Deletes every lease, in every tenant, whose deadline passed more than
lapsed_for_ms before now_ms; how many it deleted.
A lease is released by the watcher of the replica that ran its turn,
or by the reaper for a task it ends. A replica that died after its
terminal write and before its watcher released, or a child parked at
input-required that its parent never continued, kept its row for
good (audit NEW-h-s0-6). The reaper calls this on every look, with a
margin far longer than a watcher takes to see a lapse, so a watcher
still reads every lapsed lease it acts on.
The default deletes nothing: a store that does not sweep keeps every
row, as before. A store that keeps its own clock ignores now_ms, as
it does in the other methods.
§Errors
When the store cannot be written.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".