Skip to main content

process_batch_parallel

Function process_batch_parallel 

Source
pub async fn process_batch_parallel<F, Fut>(
    requests: Vec<JSONRPCRequest>,
    handler: F,
    config: ParallelBatchConfig,
) -> Result<Vec<JSONRPCResponse>>
where F: Fn(JSONRPCRequest) -> Fut + Clone + Send + Sync + 'static, Fut: Future<Output = JSONRPCResponse> + Send + 'static,
Available on non-WebAssembly only.
Expand description

Process a batch of requests in parallel while preserving order

ยงExamples

use pmcp::utils::parallel_batch::{process_batch_parallel, ParallelBatchConfig};
use pmcp::types::{JSONRPCRequest, JSONRPCResponse, RequestId};
use serde_json::json;

// Create sample requests
let requests = vec![
    JSONRPCRequest {
        jsonrpc: "2.0".to_string(),
        method: "tools.list".to_string(),
        params: None,
        id: RequestId::from(1i64),
    },
    JSONRPCRequest {
        jsonrpc: "2.0".to_string(),
        method: "resources.read".to_string(),
        params: Some(json!({"uri": "file:///example.txt"})),
        id: RequestId::from(2i64),
    },
];

// Define handler function
let handler = |request: JSONRPCRequest| async move {
    // Simulate processing
    JSONRPCResponse {
        jsonrpc: "2.0".to_string(),
        id: request.id,
        payload: pmcp::types::jsonrpc::ResponsePayload::Result(
            json!({"status": "processed", "method": request.method})
        ),
    }
};

// Process with custom config
let config = ParallelBatchConfig {
    max_concurrency: 5,
    fail_fast: false,
    request_timeout_ms: Some(10_000),
};

let responses = process_batch_parallel(requests, handler, config).await?;
assert_eq!(responses.len(), 2);