Crate request_coalescer

Crate request_coalescer 

Source
Expand description

§Request Coalescer

A library for coalescing identical asynchronous operations to prevent redundant work.

§Overview

request_coalescer provides a CoalescingService that prevents redundant work in concurrent asynchronous systems by ensuring that the underlying expensive operation is executed only once when multiple requests for the same “key” arrive simultaneously.

This is particularly useful for:

  • Deduplicating database reads or API calls for identical resources
  • Throttling access to rate-limited external services
  • Optimizing expensive computations

§Basic Usage

use request_coalescer::CoalescingService;
use std::sync::Arc;
use anyhow::Result;

async fn fetch_data(id: &str) -> Result<String> {
    // Simulate expensive operation
    tokio::time::sleep(std::time::Duration::from_millis(100)).await;
    Ok(format!("Data for {}", id))
}

#[tokio::main]
async fn main() -> Result<()> {
    // Create a new coalescing service
    let service: CoalescingService<String, String> = CoalescingService::new();

    // Execute multiple requests with the same key
    let key = "user:123".to_string();

    // These will be coalesced into a single operation
    let handle1 = {
        let svc = service.clone();
        let k = key.clone();
        tokio::spawn(async move {
            svc.execute(k, || fetch_data("user:123")).await
        })
    };

    let handle2 = {
        let svc = service.clone();
        let k = key.clone();
        tokio::spawn(async move {
            svc.execute(k, || fetch_data("user:123")).await
        })
    };

    // Both will get the same result, but the operation runs only once
    let result1 = match handle1.await {
        Ok(res) => res.map_err(|e| anyhow::anyhow!(e))?,
        Err(e) => return Err(anyhow::anyhow!(e)),
    };
    let result2 = match handle2.await {
        Ok(res) => res.map_err(|e| anyhow::anyhow!(e))?,
        Err(e) => return Err(anyhow::anyhow!(e)),
    };

    assert_eq!(result1, result2);

    Ok(())
}

§Features

  • Asynchronous: Built on Tokio for efficient async operation
  • Key-based Coalescing: Deduplicate operations based on any hashable key
  • Optional Timeouts: Set global or per-operation timeouts
  • Detailed Statistics: Track coalesced, failed, and timed-out operations
  • Error Handling: Propagate errors to all waiting clients

Re-exports§

pub use service::CoalescingService;
pub use anyhow;

Modules§

service
Core implementation of the coalescing service

Structs§

CoalescingStats
Statistics for monitoring the coalescing service.

Enums§

CoalescingError
Custom error type for coalescing operations