use dashmap::DashMap;
use std::sync::Arc;
use crate::core::traits::WebSocketConnector;
use crate::core::types::{AccountType, ExchangeId};
#[derive(Clone)]
pub(crate) struct WebSocketPool {
sockets: Arc<DashMap<(ExchangeId, AccountType), Arc<dyn WebSocketConnector>>>,
}
impl WebSocketPool {
pub(crate) fn new() -> Self {
Self {
sockets: Arc::new(DashMap::new()),
}
}
pub(crate) fn insert(
&self,
id: ExchangeId,
account_type: AccountType,
socket: Arc<dyn WebSocketConnector>,
) -> Option<Arc<dyn WebSocketConnector>> {
self.sockets.insert((id, account_type), socket)
}
pub(crate) fn get(
&self,
id: ExchangeId,
account_type: AccountType,
) -> Option<Arc<dyn WebSocketConnector>> {
self.sockets.get(&(id, account_type)).map(|e| e.value().clone())
}
pub(crate) fn remove(
&self,
id: ExchangeId,
account_type: AccountType,
) -> Option<Arc<dyn WebSocketConnector>> {
self.sockets.remove(&(id, account_type)).map(|(_, v)| v)
}
pub(crate) fn len(&self) -> usize {
self.sockets.len()
}
pub(crate) fn is_empty(&self) -> bool {
self.sockets.is_empty()
}
pub(crate) fn clear(&self) {
self.sockets.clear();
}
}
impl Default for WebSocketPool {
fn default() -> Self {
Self::new()
}
}