pub struct InMemoryTaskStore { /* private fields */ }Expand description
In-memory TaskStore backed by a pre-allocated HashMap with
secondary indexes under a single RwLock.
Suitable for testing and single-process deployments. Data is lost when the process exits.
The internal HashMap is pre-allocated to the configured max_capacity
(default 10,000) to prevent latency spikes from table resizing. Without
pre-allocation, HashMap doubles its capacity when load factor exceeds
~87.5%, triggering a full rehash of every stored entry. Pre-allocation
eliminates these unpredictable latency cliffs entirely.
§Indexing strategy
| Index | Structure | Purpose |
|---|---|---|
| Primary | HashMap<TaskId, TaskEntry> | O(1) get/save |
| Order | BTreeMap<u64, TaskId> | O(log n + page_size) update-order pagination |
| Context | HashMap<String, BTreeMap<u64, TaskId>> | O(log m + page_size) filtered list |
The order index is keyed by a monotonic per-write sequence and iterated in reverse to return most-recently-updated tasks first (spec §3.1.4) without the O(n log n) per-call sort that previously caused 20-70× regressions at 10K+ tasks. The context index avoids full-scan filtering by pre-partitioning task IDs by context, preserving the same update-order within each context.
§Eviction behavior
Eviction runs every N writes (configurable via
TaskStoreConfig::eviction_interval) and whenever the store exceeds
max_capacity. If the system goes idle (no save() calls), completed
tasks persist in memory past their TTL.
Operators should call run_eviction() periodically
(e.g. every 60 seconds via tokio::time::interval) to ensure timely
cleanup of terminal tasks during idle periods.
§The write that triggers a sweep pays for it
This paragraph used to say the sweep “runs as a background task” and that
“writers are not blocked during the O(n) cleanup”. Neither is true and both
were corrected on 2026-08-19. There is no spawn: the sweep is awaited
inside save, so the caller that triggers it waits for it, and it holds the
write lock for its whole duration, so every other writer waits too. What is
genuinely decoupled is only the lock acquisition — the insert’s write lock
is released before the sweep takes its own.
The size of that, MEASURED (debug profile, tokio multi-thread, 50,000
terminal tasks, eviction_interval 1000):
| latency | |
|---|---|
| quietest of 1,000 consecutive saves | 3.99 µs |
| slowest of the same 1,000 (the one that swept) | 4.54 ms |
One write in eviction_interval costs about 1,100× a quiet one at that
size, and it stalls every concurrent writer, not just itself. The capacity
pass is much cheaper — measured 3.5× and 3.7× on two runs at 10,000
entries — because it removes only the overflow rather than scanning for it.
This is a shape to plan for, not a defect to route around: the TTL pass is
O(n) unavoidably (finding expired entries means looking at all of them),
which is exactly why it is amortized behind eviction_interval rather than
run per write. What it means in practice is that eviction_interval is a
tail-latency knob as much as a cleanliness one, and that a deployment
sensitive to p99.9 write latency should raise it and call
run_eviction() from its own scheduler instead —
where the stall lands somewhere it chose.
§Concurrency
For high-concurrency production deployments, consider SqliteTaskStore
which uses a connection pool and row-level locking. The in-memory store
uses a single RwLock and is optimized for testing and moderate load.
Implementations§
Source§impl InMemoryTaskStore
impl InMemoryTaskStore
Sourcepub async fn run_eviction(&self)
pub async fn run_eviction(&self)
Runs background eviction of expired and over-capacity entries.
Call this periodically (e.g. every 60 seconds) to clean up terminal
tasks that would otherwise persist until the next save() call.
Source§impl InMemoryTaskStore
impl InMemoryTaskStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty in-memory task store with default configuration.
Default: max 10,000 tasks, 1-hour TTL for terminal tasks.
The internal HashMap is pre-allocated to the configured max_capacity
to prevent resize-induced latency spikes during operation.
Sourcepub fn with_config(config: TaskStoreConfig) -> Self
pub fn with_config(config: TaskStoreConfig) -> Self
Creates a new in-memory task store with custom configuration.
The internal HashMap is pre-allocated to config.max_capacity (or a
sensible default if None) to prevent resize-induced latency spikes.
Trait Implementations§
Source§impl Debug for InMemoryTaskStore
impl Debug for InMemoryTaskStore
Source§impl Default for InMemoryTaskStore
impl Default for InMemoryTaskStore
Source§impl TaskStore for InMemoryTaskStore
impl TaskStore for InMemoryTaskStore
Source§fn save_artifact_delta<'a>(
&'a self,
task: &'a Task,
delta: ArtifactDelta,
) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>>
fn save_artifact_delta<'a>( &'a self, task: &'a Task, delta: ArtifactDelta, ) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>>
Applies the delta to the stored task in place, copying only what grew.
save clones the whole task, so using it per artifact event makes a
stream cost quadratic in its own length — see
TaskStore::save_artifact_delta for the measurement. Here the work is
proportional to the appended parts instead of the accumulated ones.
Falls back to save whenever the stored record is not the one this
delta describes: absent, no artifacts, index out of range, a different
artifact at that index, or fewer parts present than the delta claims
were appended. Those are all “cannot apply safely”, and a whole-record
replace is always right — a wrong in-place edit would not be.
Source§fn save<'a>(
&'a self,
task: &'a Task,
) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>>
fn save<'a>( &'a self, task: &'a Task, ) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>>
Source§fn get<'a>(
&'a self,
id: &'a TaskId,
) -> Pin<Box<dyn Future<Output = A2aResult<Option<Task>>> + Send + 'a>>
fn get<'a>( &'a self, id: &'a TaskId, ) -> Pin<Box<dyn Future<Output = A2aResult<Option<Task>>> + Send + 'a>>
None if not found. Read moreSource§fn list<'a>(
&'a self,
params: &'a ListTasksParams,
) -> Pin<Box<dyn Future<Output = A2aResult<TaskListResponse>> + Send + 'a>>
fn list<'a>( &'a self, params: &'a ListTasksParams, ) -> Pin<Box<dyn Future<Output = A2aResult<TaskListResponse>> + Send + 'a>>
Source§fn insert_if_absent<'a>(
&'a self,
task: &'a Task,
) -> Pin<Box<dyn Future<Output = A2aResult<bool>> + Send + 'a>>
fn insert_if_absent<'a>( &'a self, task: &'a Task, ) -> Pin<Box<dyn Future<Output = A2aResult<bool>> + Send + 'a>>
Auto Trait Implementations§
impl !Freeze for InMemoryTaskStore
impl !RefUnwindSafe for InMemoryTaskStore
impl Send for InMemoryTaskStore
impl Sync for InMemoryTaskStore
impl Unpin for InMemoryTaskStore
impl UnsafeUnpin for InMemoryTaskStore
impl UnwindSafe for InMemoryTaskStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request