#pool #async #resource

simple-pool

Simple async pool for any kind of resources

15 releases

new 0.0.17 Mar 3, 2024
0.0.16 Dec 23, 2023
0.0.15 May 15, 2023
0.0.11 Nov 15, 2021
0.0.10 Sep 14, 2021

#212 in Concurrency

Download history 10/week @ 2023-12-23 2/week @ 2024-01-20 1/week @ 2024-01-27 28/week @ 2024-02-10 77/week @ 2024-02-17 66/week @ 2024-02-24

171 downloads per month
Used in 4 crates

MIT license

12KB
258 lines

simple-pool

Simple and fast async pool for any kind of resources

The idea

This is a helper library to create custom pools of anything

Crate

https://crates.io/crates/simple-pool

Example

use simple_pool::ResourcePool;
use std::sync::Arc;
use tokio::net::TcpStream;

async fn test() {
    // create a local or static resource pool
    let resource_pool: Arc<ResourcePool<TcpStream>> =
        Arc::new(ResourcePool::new());
    {
        // put 20 tcp connections there
        for _ in 0..20 {
            let client = TcpStream::connect("127.0.0.1:80").await.unwrap();
            resource_pool.append(client);
        }
    }
    let n = 1_000_000;
    for _ in 0..n {
        let pool = resource_pool.clone();
        tokio::spawn(async move {
            // gets open tcp connection as soon as one is available
            let _client = pool.get().await;
        });
    }
}

Dependencies

~0.4–6.5MB
~11K SLoC