use std::sync::Arc;
use super::{client_pool::ClientPoolInner, lease::PooledClientLease};
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,
{
inner: Arc<ClientPoolInner<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) fn new(inner: Arc<ClientPoolInner<S, P, C>>, handle_id: u64) -> Self {
Self { inner, handle_id }
}
pub async fn acquire(&mut self) -> Result<PooledClientLease<S, P, C>, ClientError> {
self.inner
.scheduler
.acquire_for_handle(Arc::clone(&self.inner), 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.inner.scheduler.deregister_handle(self.handle_id); }
}