#connection-pool #client-connection #thrift #bb8-connection #r2d2 #compatible #client-ip

thrift-pool

Easily make Connection Pools compatible with r2d2 and bb8 from any Thrift Client

17 releases (11 stable)

1.5.0 Jan 21, 2024
1.4.1 Jan 2, 2022
1.4.0 Dec 31, 2021
0.2.0 Dec 30, 2021
0.1.4 Dec 28, 2021

#3 in #r2d2

Download history 65/week @ 2023-11-27 58/week @ 2023-12-04 37/week @ 2023-12-11 2/week @ 2023-12-18 6/week @ 2024-01-15 8/week @ 2024-01-22 25/week @ 2024-02-19 24/week @ 2024-02-26 16/week @ 2024-03-04 29/week @ 2024-03-11

94 downloads per month
Used in hbase-thrift

MIT/Apache

21KB
219 lines

Thrift Pool

This library provides a simple way implement bb8 and/or r2d2 Connection Pools for any TThriftClient

Documenation

  • This library is primarily documented on its docs.rs page

  • Here is a quick example

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()?;

Examples

Dependencies

~0.6–8.5MB
~40K SLoC