#连接池 #客户端连接 #Thrift #bb8 连接 #r2d2 #兼容 #客户端IP

thrift-pool

轻松实现任何 Thrift 客户端与 r2d2 和 bb8 兼容的连接池

17 个版本 (11 个稳定版)

1.5.0 2024年1月21日
1.4.1 2022年1月2日
1.4.0 2021年12月31日
0.2.0 2021年12月30日
0.1.4 2021年12月28日

#4 in #r2d2

每月下载量 22 次
用于 hbase-thrift

MIT/Apache

21KB
219

Thrift Pool

此库提供了一种简单的方法来实现任何 TThriftClientbb8 和/或 r2d2 连接池

文档

  • 此库主要在它的 docs.rs 页面上进行了文档说明

  • 以下是一个快速示例

use thrift::protocol::{TCompactInputProtocol, TCompactOutputProtocol, TInputProtocol, TOutputProtocol};
use thrift::transport::{
    ReadHalf, TFramedReadTransport, TFramedWriteTransport, TTcpChannel, WriteHalf,
};
use thrift_pool::{MakeThriftConnectionFromAddrs, ThriftConnectionManager, ThriftConnection, FromProtocol};

// A typical generated client
struct MyThriftClient<Ip: TInputProtocol, Op: TOutputProtocol> {
    i_prot: Ip,
    o_prot: Op,
}

// implement this trait so that `MakeThriftConnectionFromAddrs` can create it
impl<Ip: TInputProtocol, Op: TOutputProtocol> FromProtocol for MyThriftClient<Ip, Op> {
    type InputProtocol = Ip;

    type OutputProtocol = Op;

    fn from_protocol(
        input_protocol: Self::InputProtocol,
        output_protocol: Self::OutputProtocol,
    ) -> Self {
        MyThriftClient {
            i_prot: input_protocol,
            o_prot: output_protocol,
        }
    }
}

// implement this trait so that `ThriftConnectionManager` can manage it
impl<Ip: TInputProtocol, Op: TOutputProtocol> ThriftConnection for MyThriftClient<Ip, Op> {
    type Error = thrift::Error;
    fn is_valid(&mut self) -> Result<(), Self::Error> {
       Ok(())
    }
    fn has_broken(&mut self) -> bool {
       false
   }
}

// the actual connection type
type Client = MyThriftClient<
    TCompactInputProtocol<TFramedReadTransport<ReadHalf<TTcpChannel>>>,
    TCompactOutputProtocol<TFramedWriteTransport<WriteHalf<TTcpChannel>>>,
>;

// this works because we implemented FromProtocol for the client
// AND
// because the `Protocol` and `Transport` types used here satisfy the contraints
let manager = ThriftConnectionManager::new(
                    MakeThriftConnectionFromAddrs::<Client, _>::new("localhost:9090")
                );

// this works because we implemented ThriftConnection for the client
let pool = r2d2::Pool::builder().build(manager)?;

// this also works after enabling the `impl-bb8` feature
let pool = bb8::Pool::builder().build(manager).await?;

// the pool can be used just like in r2d2/bb8 documentation
let mut client = pool.get()?;

示例

依赖关系

~0.6–7MB
~40K SLoC