use dashmap::DashMap;
use linera_base::time::Duration;
use super::{transport, GrpcError};
#[derive(Clone, Default)]
pub struct GrpcConnectionPool {
options: transport::Options,
channels: DashMap<String, transport::Channel>,
}
impl GrpcConnectionPool {
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> {
Ok(self
.channels
.entry(address.clone())
.or_try_insert_with(|| transport::create_channel(address, &self.options))?
.clone())
}
}