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 |
| Sorted | BTreeSet<TaskId> | O(log n + page_size) pagination |
| Context | HashMap<String, BTreeSet<TaskId>> | O(log m + page_size) filtered list |
The sorted index eliminates the O(n log n) sort in list() that
previously caused 20-70× regressions at 10K+ tasks. The context index
avoids full-scan filtering by pre-partitioning task IDs by context.
§Eviction behavior
Eviction runs as a background task every N writes (configurable via
TaskStoreConfig::eviction_interval) and whenever the store exceeds
max_capacity. The eviction sweep is decoupled from the save() write
lock so that writers are not blocked during the O(n) cleanup. However,
if the system goes idle (no save() calls), completed tasks may persist
in memory longer than 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.
§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() -> InMemoryTaskStore
pub fn new() -> InMemoryTaskStore
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) -> InMemoryTaskStore
pub fn with_config(config: TaskStoreConfig) -> InMemoryTaskStore
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§fn default() -> InMemoryTaskStore
fn default() -> InMemoryTaskStore
Source§impl TaskStore for InMemoryTaskStore
impl TaskStore for InMemoryTaskStore
Source§fn save<'a>(
&'a self,
task: &'a Task,
) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>
fn save<'a>( &'a self, task: &'a Task, ) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>
Source§fn get<'a>(
&'a self,
id: &'a TaskId,
) -> Pin<Box<dyn Future<Output = Result<Option<Task>, A2aError>> + Send + 'a>>
fn get<'a>( &'a self, id: &'a TaskId, ) -> Pin<Box<dyn Future<Output = Result<Option<Task>, A2aError>> + Send + 'a>>
None if not found. Read moreSource§fn list<'a>(
&'a self,
params: &'a ListTasksParams,
) -> Pin<Box<dyn Future<Output = Result<TaskListResponse, A2aError>> + Send + 'a>>
fn list<'a>( &'a self, params: &'a ListTasksParams, ) -> Pin<Box<dyn Future<Output = Result<TaskListResponse, A2aError>> + Send + 'a>>
Source§fn insert_if_absent<'a>(
&'a self,
task: &'a Task,
) -> Pin<Box<dyn Future<Output = Result<bool, A2aError>> + Send + 'a>>
fn insert_if_absent<'a>( &'a self, task: &'a Task, ) -> Pin<Box<dyn Future<Output = Result<bool, A2aError>> + 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