2018-06-13 21:52:23 -07:00
|
|
|
//! The `blob_fetch_stage` pulls blobs from UDP sockets and sends it to a channel.
|
|
|
|
|
2018-07-03 21:14:08 -07:00
|
|
|
use service::Service;
|
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::mpsc::channel;
|
|
|
|
use std::sync::Arc;
|
2018-07-03 21:14:08 -07:00
|
|
|
use std::thread::{self, JoinHandle};
|
2018-06-27 11:33:56 -07:00
|
|
|
use streamer::{self, BlobReceiver};
|
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 {
|
2018-12-07 19:01:28 -08:00
|
|
|
#[allow(clippy::new_ret_no_self)]
|
2018-09-18 08:02:57 -07:00
|
|
|
pub fn new(socket: Arc<UdpSocket>, exit: Arc<AtomicBool>) -> (Self, BlobReceiver) {
|
|
|
|
Self::new_multi_socket(vec![socket], 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>>,
|
2018-06-13 21:52:23 -07:00
|
|
|
exit: Arc<AtomicBool>,
|
2018-07-02 14:45:16 -07:00
|
|
|
) -> (Self, BlobReceiver) {
|
2018-08-28 16:32:40 -07:00
|
|
|
let (sender, receiver) = channel();
|
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
|
|
|
|
2018-12-07 19:01:28 -08:00
|
|
|
(Self { exit, thread_hdls }, receiver)
|
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(())
|
|
|
|
}
|
|
|
|
}
|