use linera_base::time::Duration;
use super::{transport, GrpcError};
#[derive(Clone, Default)]
pub struct GrpcConnectionPool {
options: transport::Options,
channels: papaya::HashMap<String, transport::Channel>,
}
impl GrpcConnectionPool {
pub fn new(options: transport::Options) -> Self {
Self {
options,
channels: papaya::HashMap::default(),
}
}
pub fn with_connect_timeout(mut self, connect_timeout: impl Into<Option<Duration>>) -> Self {
self.options.connect_timeout = connect_timeout.into();
self
}
pub fn with_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
self.options.timeout = timeout.into();
self
}
pub fn channel(&self, address: String) -> Result<transport::Channel, GrpcError> {
let pinned = self.channels.pin();
if let Some(channel) = pinned.get(&address) {
return Ok(channel.clone());
}
let channel = transport::create_channel(address.clone(), &self.options)?;
Ok(pinned.get_or_insert(address, channel).clone())
}
}