Thrift Pool
This library provides a simple way implement bb8
and/or r2d2 Connection Pools
for any TThriftClient
use thrift::protocol::{TCompactInputProtocol, TCompactOutputProtocol, TInputProtocol, TOutputProtocol};
use thrift::transport::{
ReadHalf, TFramedReadTransport, TFramedWriteTransport, TTcpChannel, WriteHalf,
};
use thrift_pool::{MakeThriftConnectionFromAddrs, ThriftConnectionManager, ThriftConnection, FromProtocol};
use r2d2::Pool;
struct MyThriftClient<Ip: TInputProtocol, Op: TOutputProtocol> {
i_prot: Ip,
o_prot: Op,
}
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,
}
}
}
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
}
}
type Client = MyThriftClient<
TCompactInputProtocol<TFramedReadTransport<ReadHalf<TTcpChannel>>>,
TCompactOutputProtocol<TFramedWriteTransport<WriteHalf<TTcpChannel>>>,
>;
let manager = ThriftConnectionManager::new(
MakeThriftConnectionFromAddrs::<Client, _>::new("localhost:9090")
);
let pool = Pool::builder().build(manager)?;
let mut client = pool.get()?;
Documentation
Examples