use super::{lease::PooledClientLease, scheduler::SchedulerHandle};
use crate::{
client::ClientError,
message::{DecodeWith, EncodeWith},
serializer::Serializer,
};
pub struct PoolHandle<S, P, C>
where
S: Serializer + Clone + Send + Sync + 'static,
P: bincode::Encode + Clone + Send + Sync + 'static,
C: Send + 'static,
{
scheduler: SchedulerHandle<S, P, C>,
handle_id: u64,
}
impl<S, P, C> PoolHandle<S, P, C>
where
S: Serializer + Clone + Send + Sync + 'static,
P: bincode::Encode + Clone + Send + Sync + 'static,
C: Send + 'static,
{
pub(crate) const fn new(scheduler: SchedulerHandle<S, P, C>, handle_id: u64) -> Self {
Self {
scheduler,
handle_id,
}
}
pub async fn acquire(&mut self) -> Result<PooledClientLease<S, P, C>, ClientError> {
self.scheduler.acquire(self.handle_id).await
}
pub async fn call<Req, Resp>(&mut self, request: &Req) -> Result<Resp, ClientError>
where
Req: EncodeWith<S>,
Resp: DecodeWith<S>,
{
let lease = self.acquire().await?;
lease.call(request).await
}
}
impl<S, P, C> Drop for PoolHandle<S, P, C>
where
S: Serializer + Clone + Send + Sync + 'static,
P: bincode::Encode + Clone + Send + Sync + 'static,
C: Send + 'static,
{
fn drop(&mut self) { self.scheduler.deregister(self.handle_id); }
}