a2a_protocol_server/push/
tenant_config_store.rs1use std::collections::HashMap;
12use std::future::Future;
13use std::pin::Pin;
14use std::sync::Arc;
15
16use a2a_protocol_types::error::A2aResult;
17use a2a_protocol_types::push::TaskPushNotificationConfig;
18use tokio::sync::RwLock;
19
20use super::config_store::{InMemoryPushConfigStore, PushConfigStore};
21use crate::store::tenant::TenantContext;
22
23#[derive(Debug)]
45pub struct TenantAwareInMemoryPushConfigStore {
46 stores: RwLock<HashMap<String, Arc<InMemoryPushConfigStore>>>,
47 max_tenants: usize,
48 max_configs_per_task: usize,
49}
50
51impl Default for TenantAwareInMemoryPushConfigStore {
52 fn default() -> Self {
53 Self::new()
54 }
55}
56
57impl TenantAwareInMemoryPushConfigStore {
58 #[must_use]
60 pub fn new() -> Self {
61 Self {
62 stores: RwLock::new(HashMap::new()),
63 max_tenants: 1000,
64 max_configs_per_task: 100,
65 }
66 }
67
68 #[must_use]
70 pub fn with_limits(max_tenants: usize, max_configs_per_task: usize) -> Self {
71 Self {
72 stores: RwLock::new(HashMap::new()),
73 max_tenants,
74 max_configs_per_task,
75 }
76 }
77
78 async fn get_store(&self) -> A2aResult<Arc<InMemoryPushConfigStore>> {
80 let tenant = TenantContext::partition();
81
82 {
83 let stores = self.stores.read().await;
84 if let Some(store) = stores.get(&tenant) {
85 return Ok(Arc::clone(store));
86 }
87 }
88
89 let mut stores = self.stores.write().await;
90 if let Some(store) = stores.get(&tenant) {
91 return Ok(Arc::clone(store));
92 }
93
94 if stores.len() >= self.max_tenants {
95 return Err(a2a_protocol_types::error::A2aError::internal(format!(
96 "tenant limit exceeded: max {} tenants",
97 self.max_tenants
98 )));
99 }
100
101 let store = Arc::new(InMemoryPushConfigStore::with_max_configs_per_task(
102 self.max_configs_per_task,
103 ));
104 stores.insert(tenant, Arc::clone(&store));
105 drop(stores);
106 Ok(store)
107 }
108
109 async fn get_existing_store(&self) -> Option<Arc<InMemoryPushConfigStore>> {
116 let tenant = TenantContext::partition();
117 self.stores.read().await.get(&tenant).map(Arc::clone)
118 }
119
120 pub async fn tenant_count(&self) -> usize {
122 self.stores.read().await.len()
123 }
124
125 pub async fn erase_caller(&self, caller: &str) -> crate::store::caller_keys::CallerErasure {
128 let mut stores = self.stores.write().await;
129 let theirs: Vec<String> = stores
130 .keys()
131 .filter(|p| crate::store::caller_keys::is_callers_partition(p, caller))
132 .cloned()
133 .collect();
134 let mut done = crate::store::caller_keys::CallerErasure::default();
135 for partition in theirs {
136 if let Some(store) = stores.remove(&partition) {
137 let configs = store.count().await.ok().flatten().unwrap_or(0);
138 done.rows += u64::try_from(configs).unwrap_or(u64::MAX);
139 done.partitions += 1;
140 }
141 }
142 done
143 }
144}
145
146#[allow(clippy::manual_async_fn)]
147impl PushConfigStore for TenantAwareInMemoryPushConfigStore {
148 fn is_process_local(&self) -> bool {
149 true
150 }
151
152 fn isolates_tenants(&self) -> bool {
153 true
154 }
155
156 fn set<'a>(
157 &'a self,
158 config: TaskPushNotificationConfig,
159 ) -> Pin<Box<dyn Future<Output = A2aResult<TaskPushNotificationConfig>> + Send + 'a>> {
160 Box::pin(async move {
161 let store = self.get_store().await?;
162 store.set(config).await
163 })
164 }
165
166 fn get<'a>(
167 &'a self,
168 task_id: &'a str,
169 id: &'a str,
170 ) -> Pin<Box<dyn Future<Output = A2aResult<Option<TaskPushNotificationConfig>>> + Send + 'a>>
171 {
172 Box::pin(async move {
173 match self.get_existing_store().await {
175 Some(store) => store.get(task_id, id).await,
176 None => Ok(None),
177 }
178 })
179 }
180
181 fn list<'a>(
182 &'a self,
183 task_id: &'a str,
184 ) -> Pin<Box<dyn Future<Output = A2aResult<Vec<TaskPushNotificationConfig>>> + Send + 'a>> {
185 Box::pin(async move {
186 match self.get_existing_store().await {
188 Some(store) => store.list(task_id).await,
189 None => Ok(Vec::new()),
190 }
191 })
192 }
193
194 fn delete<'a>(
195 &'a self,
196 task_id: &'a str,
197 id: &'a str,
198 ) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>> {
199 Box::pin(async move {
200 match self.get_existing_store().await {
203 Some(store) => store.delete(task_id, id).await,
204 None => Ok(()),
205 }
206 })
207 }
208
209 fn count(&self) -> Pin<Box<dyn Future<Output = A2aResult<Option<usize>>> + Send + '_>> {
210 Box::pin(async move {
211 match self.get_existing_store().await {
214 Some(store) => store.count().await,
215 None => Ok(Some(0)),
216 }
217 })
218 }
219}
220
221#[cfg(test)]
222#[path = "tenant_config_store_tests.rs"]
223mod tests;