tokio-task-pool

Task pool for Tokio Runtime

https://crates.io/crates/tokio-task-pool

The problem

A typical pattern

rust,ignore loop { let (socket, _) = listener.accept().await.unwrap(); tokio::spawn(async move { process(socket).await; }); }

is actually an anti-pattern which may break your production.

Why? Because this pattern behaves equally to an unbounded channel. If the producer has higher rate than consumers, it floods runtime with tasks and sooner or later causes memory overflow.

Solution

Features provided

Code example

Simple spawning is pretty similar to tokio::spawn, but async because the producer must be blocked until there is an empty task slot in the pool:

```rust use std::time::Duration; use tokiotaskpool::Pool;

let pool = Pool::bounded(5) .withspawntimeout(Duration::fromsecs(5)) .withruntimeout(Duration::fromsecs(10)); pool.spawn(async move { // do some job }); ```

More features

Refer to the crate documentation.