solana/src/fetch_stage.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

2018-05-29 10:18:12 -07:00
//! The `fetch_stage` batches input from a UDP socket and sends it to a channel.
2018-12-07 19:16:27 -08:00
use crate::service::Service;
use crate::streamer::{self, PacketReceiver};
2018-05-29 10:18:12 -07:00
use std::net::UdpSocket;
2018-07-09 13:53:18 -07:00
use std::sync::atomic::{AtomicBool, Ordering};
2018-05-29 10:18:12 -07:00
use std::sync::mpsc::channel;
use std::sync::Arc;
use std::thread::{self, JoinHandle};
2018-05-29 10:18:12 -07:00
pub struct FetchStage {
2018-07-09 13:53:18 -07:00
exit: Arc<AtomicBool>,
thread_hdls: Vec<JoinHandle<()>>,
2018-05-29 10:18:12 -07:00
}
impl FetchStage {
#[allow(clippy::new_ret_no_self)]
pub fn new(sockets: Vec<UdpSocket>, exit: Arc<AtomicBool>) -> (Self, PacketReceiver) {
let tx_sockets = sockets.into_iter().map(Arc::new).collect();
Self::new_multi_socket(tx_sockets, exit)
}
pub fn new_multi_socket(
sockets: Vec<Arc<UdpSocket>>,
exit: Arc<AtomicBool>,
) -> (Self, PacketReceiver) {
let (sender, receiver) = channel();
let thread_hdls: Vec<_> = sockets
.into_iter()
.map(|socket| streamer::receiver(socket, exit.clone(), sender.clone(), "fetch-stage"))
.collect();
2018-05-29 10:18:12 -07: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-05-29 10:18:12 -07:00
}
}
impl Service for FetchStage {
type JoinReturnType = ();
fn join(self) -> thread::Result<()> {
for thread_hdl in self.thread_hdls {
thread_hdl.join()?;
}
Ok(())
}
}