async-rs / async-rs/parallel-stream

Method: limit

Open
#1 1 comment 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
96
Forks
13
PR merge metrics
No merged PRs in 30d

Description

It should be possible to limit the max concurrency. This likely needs to be built into the `ParallelStream` trait itself, with each impl being responsible for carrying it.

The default limit should be unlimited, with the executor and runtime scheduling resources.

## API Outline

```rust
trait ParallelStream {
fn limit(self, limit: impl Into>) -> Self;
}
```

## Usage

```rust
// Set max limit to 5
let out: Vec = parallel_stream::repeat(5)
.take(100)
.limit(5) // <-- set max concurrency
.map(async |n| n * n)
.collect()
.await;

// Remove max limit
let out: Vec = parallel_stream::repeat(5)
.take(100)
.limit(None) // <-- set max concurrency
.map(async |n| n * n)
.collect()
.await;
```

## Naming

The name `limit` is inspired by the parameter name of [`for_each_concurrent`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.for_each_concurrent). It seems small and to the point, though not particularly attached if there's something different that's concise yet to the point.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.