pub struct Cluster {
shutdown: tokio::sync::watch::Sender<Option<f64>>,
threads: Vec<std::thread::JoinHandle<()>>,
addresses: Vec<std::net::SocketAddr>,
}
impl Cluster {
pub fn cores() -> usize {
std::thread::available_parallelism().map(|count| count.get()).unwrap_or(1)
}
pub fn new(shutdown: tokio::sync::watch::Sender<Option<f64>>, threads: Vec<std::thread::JoinHandle<()>>, addresses: Vec<std::net::SocketAddr>) -> Self {
Self { shutdown, threads, addresses }
}
pub fn address(&self) -> Option<std::net::SocketAddr> {
self.addresses.first().copied()
}
pub fn addresses(&self) -> &[std::net::SocketAddr] {
&self.addresses
}
pub fn workers(&self) -> usize {
self.threads.len()
}
pub fn close(self, timeout: Option<f64>) {
let _ = self.shutdown.send(timeout);
for thread in self.threads {
let _ = thread.join();
}
}
}