a2a_protocol_server/builder/
multi_tenant.rs1use std::sync::Arc;
9
10use super::{CallerIsolation, RequestHandlerBuilder, TenantIsolation};
11use crate::error::{ServerError, ServerResult};
12use crate::tenant_resolver::TenantResolver;
13
14fn refuse(why: &str) -> ServerResult<()> {
15 Err(ServerError::Config(format!(
16 "the multi-tenant profile {why}"
17 )))
18}
19
20impl RequestHandlerBuilder {
21 #[must_use]
34 pub fn with_authorized_tenant_resolver(mut self, resolver: impl TenantResolver) -> Self {
35 self.tenant_resolver = Some(Arc::new(resolver));
36 self.tenant_resolver_authorized = true;
37 self
38 }
39
40 pub(super) fn check_multi_tenant(&self) -> ServerResult<()> {
42 if !self.interceptors.has_authenticator() {
43 return refuse(
44 "needs an authenticating interceptor: otherwise no request names a caller, \
45 and no tenant can be authorized for one",
46 );
47 }
48 if self.tenant_resolver.is_none() || !self.tenant_resolver_authorized {
49 return refuse(
50 "needs a tenant resolver the deployment asserts is authorized \
51 (with_authorized_tenant_resolver): a tenant the client chose isolates nothing",
52 );
53 }
54 if self.caller_isolation == Some(CallerIsolation::Shared) && self.task_store.is_none() {
55 return refuse(
56 "was told to share tasks across callers within a tenant, which needs a \
57 tenant-aware task store you supply: the default keeps one set of records \
58 for every tenant",
59 );
60 }
61 let task_partitions = self
65 .task_store
66 .as_ref()
67 .is_none_or(|s| s.isolates_tenants());
68 let push_partitions = self
71 .push_config_store
72 .as_ref()
73 .is_none_or(|s| s.isolates_tenants());
74 if self.tenant_isolation == TenantIsolation::SharedAccepted
75 || !(task_partitions && push_partitions)
76 {
77 return refuse(
78 "needs a task store and a push-config store that partition by tenant (the \
79 TenantAware* stores) and does not accept stores that share records across \
80 tenants (accept_unisolated_tenants)",
81 );
82 }
83 let Some(config) = &self.tenant_config else {
84 return refuse(
85 "needs finite per-tenant budgets (with_tenant_config): a tenant with no \
86 concurrency limit can take every executor slot from the others",
87 );
88 };
89 let unbounded = std::iter::once(("the default", &config.default)).chain(
90 config
91 .overrides
92 .iter()
93 .map(|(tenant, limits)| (tenant.as_str(), limits)),
94 );
95 for (whose, limits) in unbounded {
96 if limits.max_concurrent_tasks.is_none() {
97 return Err(ServerError::Config(format!(
98 "the multi-tenant profile needs a concurrency limit for every tenant \
99 (TenantLimits::with_max_concurrent_tasks), and {whose} has none"
100 )));
101 }
102 }
103 Ok(())
104 }
105}