pub struct ToolConcurrencyManager { /* private fields */ }Expand description
Core traits and types.
Always available regardless of feature flags. Includes:
Agent- The fundamental trait for all agentsTool/Toolset- For extending agents with capabilitiesSession/State- For managing conversation contextEvent- For streaming agent responsesAdkError/Result- Unified error handling Manages semaphores for tool concurrency enforcement.
Created from a ToolConcurrencyConfig, the manager pre-allocates semaphores
for the global limit and each per-tool override. Use acquire
to obtain a ConcurrencyPermit before executing a tool.
§Example
use adk_core::{
BackpressurePolicy, ToolConcurrencyConfig, ToolConcurrencyManager,
};
use std::collections::HashMap;
let config = ToolConcurrencyConfig {
max_concurrency: Some(5),
per_tool: HashMap::from([("expensive_tool".to_string(), 1)]),
backpressure: BackpressurePolicy::Queue,
};
let manager = ToolConcurrencyManager::new(&config);
// Only 1 "expensive_tool" can run at a time
let permit = manager.acquire("expensive_tool").await.unwrap();
// ... run tool ...
drop(permit);
// Other tools use the global limit of 5
let permit = manager.acquire("cheap_tool").await.unwrap();
drop(permit);Implementations§
Source§impl ToolConcurrencyManager
impl ToolConcurrencyManager
Sourcepub fn new(config: &ToolConcurrencyConfig) -> ToolConcurrencyManager
Available on crate feature runner only.
pub fn new(config: &ToolConcurrencyConfig) -> ToolConcurrencyManager
runner only.Create a new manager from the given configuration.
Allocates semaphores based on the config:
- A global semaphore with
max_concurrencypermits (if set) - Per-tool semaphores for each entry in
per_tool
§Example
use adk_core::{ToolConcurrencyConfig, ToolConcurrencyManager};
let config = ToolConcurrencyConfig {
max_concurrency: Some(10),
..Default::default()
};
let manager = ToolConcurrencyManager::new(&config);Sourcepub fn has_limits(&self) -> bool
Available on crate feature runner only.
pub fn has_limits(&self) -> bool
runner only.Returns true if this manager has any concurrency limits configured.
When no limits are configured (no global limit and no per-tool overrides),
calling acquire always succeeds immediately with no
semaphore enforcement.
Sourcepub async fn acquire(
&self,
tool_name: &str,
) -> Result<ConcurrencyPermit, AdkError>
Available on crate feature runner only.
pub async fn acquire( &self, tool_name: &str, ) -> Result<ConcurrencyPermit, AdkError>
runner only.Acquire a permit for the named tool.
If a per-tool override exists for tool_name, the per-tool semaphore is used.
Otherwise, the global semaphore is used (if configured). When neither a per-tool
override nor a global limit is configured, a permit is returned immediately with
no semaphore enforcement.
§Errors
Returns AdkError when BackpressurePolicy::Fail is configured and no
permit is immediately available.
§Example
use adk_core::{
BackpressurePolicy, ToolConcurrencyConfig, ToolConcurrencyManager,
};
let config = ToolConcurrencyConfig {
max_concurrency: Some(1),
backpressure: BackpressurePolicy::Fail,
..Default::default()
};
let manager = ToolConcurrencyManager::new(&config);
// First acquire succeeds
let permit1 = manager.acquire("tool_a").await.unwrap();
// Second acquire fails immediately (Fail policy)
let result = manager.acquire("tool_b").await;
assert!(result.is_err());
drop(permit1);