This commit is contained in:
parent
fedf7598c9
commit
2d47f6e7e0
13
LICENSE-APACHE
Normal file
13
LICENSE-APACHE
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
Copyright 2025 Kasper Juul Hermansen
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
8
LICENSE-MIT
Normal file
8
LICENSE-MIT
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
Copyright 2025 Kasper Juul Hermansen @nonothing
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
99
README.md
Normal file
99
README.md
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
# noworkers
|
||||||
|
|
||||||
|
A small, ergonomic Rust crate for spawning and supervising groups of asynchronous “workers” on Tokio.
|
||||||
|
Manage concurrent tasks with optional limits, cancellation, and first-error propagation.
|
||||||
|
|
||||||
|
Inpired by golang (errgroups)
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Unlimited or bounded concurrency** via `with_limit(usize)`.
|
||||||
|
- **Cancellation support** with [`tokio_util::sync::CancellationToken`]—either external (`with_cancel`) or task-driven (`with_cancel_task`).
|
||||||
|
- **First-error wins**: the first worker to fail cancels the rest and reports its error.
|
||||||
|
- **Graceful shutdown**: `.wait()` awaits all workers, cancels any in-flight tasks, and returns the first error (if any).
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Add to your `Cargo.toml`:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[dependencies]
|
||||||
|
noworkers = "0.1"
|
||||||
|
````
|
||||||
|
|
||||||
|
Then in your code:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use noworkers::Workers;
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Quick Example
|
||||||
|
|
||||||
|
```rust,no_run
|
||||||
|
use noworkers::Workers;
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
// Create a worker group with up to 5 concurrent tasks
|
||||||
|
let mut workers = Workers::new();
|
||||||
|
|
||||||
|
workers
|
||||||
|
.with_limit(5)
|
||||||
|
.with_cancel(&CancellationToken::new());
|
||||||
|
|
||||||
|
// Spawn 10 async jobs
|
||||||
|
for i in 0..10 {
|
||||||
|
workers.add(move |cancel_token| async move {
|
||||||
|
// Respect cancellation, or not, if you don't care about blocking forever
|
||||||
|
tokio::select! {
|
||||||
|
_ = tokio::time::sleep(tokio::time::Duration::from_secs(1)) => {
|
||||||
|
println!("Job {i} done");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
_ = cancel_token.cancelled() => {
|
||||||
|
println!("Job {i} cancelled");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for all to finish or for the first error
|
||||||
|
workers.wait().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## API Overview
|
||||||
|
|
||||||
|
* `Workers::new() -> Workers`
|
||||||
|
Create a fresh worker group.
|
||||||
|
|
||||||
|
* `with_limit(limit: usize) -> &mut Self`
|
||||||
|
Bound in-flight tasks to `limit`, back-pressuring `.add()` calls when full.
|
||||||
|
|
||||||
|
* `with_cancel(token: &CancellationToken) -> &mut Self`
|
||||||
|
Tie this group’s lifetime to an external token.
|
||||||
|
|
||||||
|
* `with_cancel_task(fut: impl Future<Output=()>) -> &mut Self`
|
||||||
|
Spawn a task that, when it completes, cancels the group.
|
||||||
|
|
||||||
|
* `add<F, Fut>(&self, f: F) -> anyhow::Result<()>`
|
||||||
|
Spawn a new worker. `f` is a closure taking a child `CancellationToken` and returning `Future<Output=anyhow::Result<()>>`.
|
||||||
|
|
||||||
|
* `wait(self) -> anyhow::Result<()>`
|
||||||
|
Await all workers. Returns the first error (if any), after cancelling in-flight workers.
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
* The **first** worker to return `Err(_)` wins: its error is sent on a oneshot and all others are cancelled.
|
||||||
|
* Subsequent errors are ignored.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Dual-licensed under **MIT** or **Apache-2.0**.
|
||||||
|
See [LICENSE-MIT](LICENSE-MIT) and [LICENSE-APACHE](LICENSE-APACHE) for details.
|
||||||
|
```
|
||||||
|
|
@ -1,7 +1,12 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "noworkers"
|
name = "noworkers"
|
||||||
version = "0.1.0"
|
edition = "2024"
|
||||||
edition = "2021"
|
readme = "../../README.md"
|
||||||
|
version.workspace = true
|
||||||
|
license = "MIT or APACHE"
|
||||||
|
repository = "https://git.front.kjuulh.io/kjuulh/noworkers"
|
||||||
|
authors = ["kjuulh <contact@kasperhermansen.com>"]
|
||||||
|
description = "A small asyncronous worker pool manages thread pool limiting, cancellation and error propogation, inspired by golangs errgroup (requires tokio)"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user