9 unstable releases (3 breaking)

Uses new Rust 2024

0.4.1 Sep 19, 2025
0.4.0 Aug 26, 2025
0.3.0 Aug 25, 2025
0.2.4 Aug 21, 2025
0.1.0 Jul 30, 2025

#12 in #conn

Download history 1/week @ 2025-10-22

681 downloads per month
Used in tcp-relay

MIT license

8KB
140 lines

tcp连接池

async fn main() {
    init_log();

    let addr = Address::from("baidu.com:80");

    let pool = Arc::new(tcp_pool::Pool::default());

    // 池里加bs
    pool.add_backend(addr.clone());

    // 设置最大数量
    pool.set_max_conn(Some(1));

    // 获取第一个连接,应该成功
    let r1 = pool.clone().get("").await;
    assert_eq!(r1.is_ok(), true);

    // 获取第二个应该会报池满了的错误
    let r2 = pool.clone().get("").await;
    assert_eq!(r2.is_err(), true);
    if let Err(net_pool::Error::PoolFull) = r2 {
    } else {
        assert!(false);
    }

    // 当前计数为1
    assert_eq!(pool.get_cur_conn(), 1);
    // 释放r1
    drop(r1);
    // 记数回归0
    assert_eq!(pool.get_cur_conn(), 0);
}

Dependencies

~2–14MB
~99K SLoC