2018-06-13 21:52:23 -07:00
|
|
|
//! The `blob_fetch_stage` pulls blobs from UDP sockets and sends it to a channel.
|
|
|
|
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::service::Service;
|
2019-01-31 13:43:22 -08:00
|
|
|
use crate::streamer::{self, BlobSender};
|
2018-06-13 21:52:23 -07:00
|
|
|
use std::net::UdpSocket;
|
2018-07-09 13:53:18 -07:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-06-13 21:52:23 -07:00
|
|
|
use std::sync::Arc;
|
2018-07-03 21:14:08 -07:00
|
|
|
use std::thread::{self, JoinHandle};
|
2018-06-13 21:52:23 -07:00
|
|
|
|
|
|
|
pub struct BlobFetchStage {
|
2018-07-09 13:53:18 -07:00
|
|
|
exit: Arc<AtomicBool>,
|
2018-07-03 21:14:08 -07:00
|
|
|
thread_hdls: Vec<JoinHandle<()>>,
|
2018-06-13 21:52:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlobFetchStage {
|
2019-01-31 13:43:22 -08:00
|
|
|
pub fn new(socket: Arc<UdpSocket>, sender: &BlobSender, exit: Arc<AtomicBool>) -> Self {
|
|
|
|
Self::new_multi_socket(vec![socket], sender, exit)
|
2018-06-13 21:52:23 -07:00
|
|
|
}
|
|
|
|
pub fn new_multi_socket(
|
2018-08-28 16:32:40 -07:00
|
|
|
sockets: Vec<Arc<UdpSocket>>,
|
2019-01-31 13:43:22 -08:00
|
|
|
sender: &BlobSender,
|
2018-06-13 21:52:23 -07:00
|
|
|
exit: Arc<AtomicBool>,
|
2019-01-31 13:43:22 -08:00
|
|
|
) -> Self {
|
2018-06-13 21:52:23 -07:00
|
|
|
let thread_hdls: Vec<_> = sockets
|
|
|
|
.into_iter()
|
2018-09-18 08:02:57 -07:00
|
|
|
.map(|socket| streamer::blob_receiver(socket, exit.clone(), sender.clone()))
|
|
|
|
.collect();
|
2018-06-13 21:52:23 -07:00
|
|
|
|
2019-01-31 13:43:22 -08:00
|
|
|
Self { exit, thread_hdls }
|
2018-07-09 13:53:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(&self) {
|
|
|
|
self.exit.store(true, Ordering::Relaxed);
|
2018-06-13 21:52:23 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 21:14:08 -07:00
|
|
|
|
|
|
|
impl Service for BlobFetchStage {
|
2018-09-13 14:00:17 -07:00
|
|
|
type JoinReturnType = ();
|
2018-07-03 21:14:08 -07:00
|
|
|
|
|
|
|
fn join(self) -> thread::Result<()> {
|
2018-09-13 14:00:17 -07:00
|
|
|
for thread_hdl in self.thread_hdls {
|
2018-07-03 21:14:08 -07:00
|
|
|
thread_hdl.join()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|