use std::{collections::VecDeque, sync::Mutex};
use crate::{Error, stream::Stream};
#[derive(Debug)]
pub struct StreamPool {
streams: Mutex<VecDeque<Stream>>,
}
impl StreamPool {
pub fn new(pool_capacity: usize) -> Self {
Self {
streams: Mutex::new(VecDeque::with_capacity(pool_capacity)),
}
}
pub async fn push(&self, mut stream: Stream) -> Result<(), Error> {
{
let mut streams = self.streams.lock().unwrap();
if streams.len() < streams.capacity() {
streams.push_back(stream);
return Ok(());
}
}
stream.safe_close_notify();
_ = stream.close().await;
Err(Error::StreamPoolFull)
}
pub fn pop(&self) -> Option<Stream> {
self.streams.lock().unwrap().pop_front()
}
pub async fn close(&self) {
while let Some(mut s) = self.pop() {
s.safe_close_notify();
_ = s.close().await;
}
}
}