3 releases

new 0.0.3 Oct 26, 2024
0.0.2 Oct 2, 2024
0.0.1 Sep 28, 2024

#95 in Concurrency

Download history 156/week @ 2024-09-23 193/week @ 2024-09-30 13/week @ 2024-10-07 8/week @ 2024-10-14 105/week @ 2024-10-21

344 downloads per month

Custom license

73KB
1.5K SLoC

pumps-rs

Eager streams for Rust. If a stream allows water to flow down the hill, a pump forces it up.

Crates.io Documentation

Futures stream api is awesome, but has unfortunate issues

This crate offers an alternative approach for rust async pipelining.

Main features:

  • Designed for common async pipelining needs in heart
  • Explicit concurrency, ordering, and backpressure control
  • Eager - work is done before downstream methods consumes it
  • builds on top of Rust async tools as tasks and channels.
  • For now only supports the Tokio async runtime
  • TBA
    • additional operators

Example:

let (mut output_receiver, join_handle) = pumps::Pipeline::from_iter(urls)
    .map(get_json, Concurrency::concurrent_ordered(5))
    .backpressure(100)
    .map(download_heavy_resource, Concurrency::serial())
    .filter_map(run_algorithm, Concurrency::concurrent_unordered(5))
    .map(save_to_db, Concurrency::concurrent_unordered(100))
    .build();

while let Some(output) = output_receiver.recv().await {
    println!("{output}");
}

Pumps

A Pump is a wrapper around a common async programming (or rather multithreading) pattern - concurrent work is split into several tasks that communicate with each other using channels

let (sender0, mut receiver0) = mpsc::channel(100);
let (sender1, mut receiver1) = mpsc::channel(100);
let (sender2, mut receiver2) = mpsc::channel(100);

tokio::spawn(async move {
    while let Some(x) = receiver0.recv().await {
        let output = work0(x).await;
        sender1.send(output).await.unwrap();
    }
});

tokio::spawn(async move {
    while let Some(x) = receiver1.recv().await {
        let output = work1(x).await;
        sender2.send(output).await.unwrap();
    }
});

// send data to input channel
send_input(sender0).await;

while let Some(output) = receiver2.recv().await {
    println!("done with {}", output);
}

A 'Pump' is one step of such pipeline - a task and input/output channel. For example the Map Pump spawns a task, receives input via a Receiver, runs an async function, and sends its output to a Sender

A Pipeline is a chain of Pumps. Each pump receives its input from the output channel of its predecessor

Creation

// from channel
let (mut output_receiver, join_handle) = Pipeline::from(tokio_channel);

// from a stream
let (mut output_receiver, join_handle) = Pipeline::from_stream(stream);

// create an iterator
let (mut output_receiver, join_handle) = Pipeline::from_iter(iter);

The .build() method returns a touple of a tokio::sync::mpsc::Receiver and a join handle to the internally spawned tasks

Concurrency control

Each Pump operation receives a Concurrency struct that defines the concurrency characteristics of the operation.

  • serial execution - Concurrency::serial()
  • concurrent execution - Concurrency::concurrent_ordered(n), Concurrency::concurrent_unordered(n)

Backpressure

Backpressure defines the amount of unconsumed data that can accumulate in memory. Without backpressure an eger operation will keep processing data and storing it in memory. A slow downstream consumer will result with unbounded memory usage. On the other hand, if we limit the in-memory buffering to 1, slow downstream consumer will often hang processing and introduce inefficiencies to the pipeline.

By default, the output channels of the various supplied pumps are with buffer size 1. Adding backpressure before potentially slow operations can improve processing efficiency.

The .backpressure(n) operation limits the output channel of a Pump allowing it to stop processing data until the output channel have been consumed. The .backpressure_with_relief_valve(n) operation is similar to backpressure(n) but instead of blocking the input channel it drops the oldest inputs.

Visual comparison with streams

To understand the difference in concurrency characteristics between Pumps and Stream lets visualize similar pipelines in both frameworks. We will visualize a series of 3 ordered concurrent async jobs. Each square in the animation represents a single unit of work that flows between the different pipeline stages. For a deeper dive into the visualization check out this blog post

With streams the pipeline looks something like:

input_stream
  .map(async_job)
  .buffered(3)
  .map(async_job)
  .buffered(3)
  .map(longer_async_job)
  .buffered(2)

With Pumps it looks like:

pumps::Pipeline::from_iter(input)
    .map(async_job, Concurrency::concurrent_ordered(3))
    .map(async_job, Concurrency::concurrent_ordered(3))
    .map(longer_async_job, Concurrency::concurrent_ordered(2))
    .build();

main differences:

  • Using streams, futures from different stages of the pipeline do not run concurrently. Using Pumps everything runs concurrently.
  • Using streams, each stage waits for a downstream method to poll_next it before taking new work. Using Pumps each stage takes new jobs eagerly.
  • Pumps allows for a configurable backpressure. The effect of this can be seen when a heavy task is slow to take new work. The previous stages continue to work until it accumulates backpressure number of results.

Dependencies

~3–9.5MB
~73K SLoC