Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/mofa-foundation/src/agent/tools/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ use crate::agent::components::tool::{SimpleTool, ToolCategory};
// HttpTool
// ============================================================================

/// Shared HTTP client for connection pool reuse across all invocations.
/// reqwest::Client holds an internal connection pool, DNS resolver, and TLS cache
/// that are expensive to recreate. This static ensures keep-alive reuse and efficient
/// resource management across the lifetime of the agent.
static HTTP_CLIENT: once_cell::sync::Lazy<reqwest::Client> =
once_cell::sync::Lazy::new(|| reqwest::Client::new());

/// Make HTTP GET or POST requests and return the response body.
///
/// Requires network access. The LLM supplies the URL, method, optional headers,
Expand Down Expand Up @@ -96,7 +103,7 @@ impl SimpleTool for HttpTool {
};

let method = input.get_str("method").unwrap_or("GET").to_uppercase();
let client = reqwest::Client::new();
let client = &*HTTP_CLIENT;

let mut builder = match method.as_str() {
"GET" => client.get(&url),
Expand Down
20 changes: 18 additions & 2 deletions crates/mofa-runtime/src/agent/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tokio::sync::{RwLock, Semaphore};
use tokio::time::timeout;
use tracing::Instrument;

Expand Down Expand Up @@ -566,20 +566,36 @@ impl ExecutionEngine {

/// 并行执行多个 Agent
/// Execute multiple agents in parallel
/// Respects concurrency limits to prevent resource exhaustion.
/// Default concurrency: 10 concurrent tasks
pub async fn execute_parallel(
&self,
executions: Vec<(String, AgentInput)>,
options: ExecutionOptions,
) -> Vec<AgentResult<ExecutionResult>> {
// Use Semaphore to limit concurrent task spawning
// Default: 10 concurrent tasks (respects RuntimeConfig::max_concurrent_tasks default)
let max_concurrent = 10;
let semaphore = Arc::new(Semaphore::new(max_concurrent));
let mut handles = Vec::new();

for (agent_id, input) in executions {
let engine = self.clone();
let opts = options.clone();
let sem = semaphore.clone();

let span = tracing::info_span!("agent.parallel", agent_id = %agent_id);

// Acquire a permit before spawning the task
let permit = sem.acquire().await.expect("semaphore should never be closed");

let handle = tokio::spawn(
async move { engine.execute(&agent_id, input, opts).await }.instrument(span),
async move {
// Hold the permit for the entire execution
let _permit = permit;
engine.execute(&agent_id, input, opts).await
}
.instrument(span),
);

handles.push(handle);
Expand Down
Loading