#pool #async-pool #orm #database

fast_pool

基于通道的快速池

3个不稳定版本

0.2.0 2023年12月28日
0.1.9 2023年12月28日

缓存类别中排名97

Download history 484/week @ 2024-03-13 458/week @ 2024-03-20 446/week @ 2024-03-27 271/week @ 2024-04-03 280/week @ 2024-04-10 250/week @ 2024-04-17 301/week @ 2024-04-24 357/week @ 2024-05-01 332/week @ 2024-05-08 231/week @ 2024-05-15 331/week @ 2024-05-22 549/week @ 2024-05-29 415/week @ 2024-06-05 442/week @ 2024-06-12 395/week @ 2024-06-19 577/week @ 2024-06-26

每月下载量1,910
20个crate中使用(通过rbdc-pool-fast

Apache-2.0

12KB
208

fast_pool

unsafe forbidden GitHub release

基于通道的快速异步池

  • 支持 get()get_timeout()state() 方法
  • 支持原子最大打开数(可自由调整大小)
  • 基于 flume

为什么是fast_pool?

  • fast get()方法性能
//windows:
//---- bench_pool stdout ----
//use Time: 4.0313ms ,each:40 ns/op
//use QPS: 24749412 QPS/s
//macos:
//---- bench_pool stdout ----
// use Time: 6.373708ms ,each:63 ns/op
// use QPS: 15683710 QPS/s

如何使用它?

  • 添加toml
fast_pool="0.1"
async-trait = "0.1"
tokio = {version = "1",features = ["time","rt-multi-thread","macros"]}
  • 实现trait
use std::ops::{DerefMut};
use std::time::Duration;
use async_trait::async_trait;
use fast_pool::{Manager, Pool};

#[derive(Debug)]
pub struct TestManager {}

impl Manager for TestManager {
    type Connection = String;
    type Error = String;

    async fn connect(&self) -> Result<Self::Connection, Self::Error> {
        Ok("conn".to_string())
    }

    async fn check(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
        //check should use conn.ping()
        if conn == "error" {
            return Err(Self::Error::from("error".to_string()));
        }
        Ok(())
    }
}

#[tokio::main]
async fn main() {
    let p = Pool::new(TestManager {});
    println!("status = {}",p.state());
    p.set_max_open(10);
    println!("status = {}",p.state());

    let mut conn = p.get().await.unwrap();
    println!("conn = {}",conn.deref_mut());
    let mut conn = p.get_timeout(Some(Duration::from_secs(1))).await.unwrap();
    println!("conn = {}",conn.deref_mut());
}

依赖项

~2.6–4MB
~70K SLoC