solana/core/src/fetch_stage.rs

61 lines
1.6 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, PacketSender};
2018-05-29 10:18:12 -07:00
use std::net::UdpSocket;
use std::sync::atomic::AtomicBool;
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 {
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>,
forwarder_sockets: Vec<UdpSocket>,
exit: &Arc<AtomicBool>,
) -> (Self, PacketReceiver) {
let (sender, receiver) = channel();
(
Self::new_with_sender(sockets, forwarder_sockets, exit, &sender),
receiver,
)
}
pub fn new_with_sender(
sockets: Vec<UdpSocket>,
forwarder_sockets: Vec<UdpSocket>,
2019-03-04 19:53:50 -08:00
exit: &Arc<AtomicBool>,
sender: &PacketSender,
) -> Self {
let tx_sockets = sockets.into_iter().map(Arc::new).collect();
Self::new_multi_socket(tx_sockets, exit, &sender)
}
fn new_multi_socket(
sockets: Vec<Arc<UdpSocket>>,
2019-03-04 19:53:50 -08:00
exit: &Arc<AtomicBool>,
sender: &PacketSender,
) -> Self {
let thread_hdls: Vec<_> = sockets
.into_iter()
2019-03-04 20:50:02 -08:00
.map(|socket| streamer::receiver(socket, &exit, sender.clone(), "fetch-stage"))
.collect();
2018-05-29 10:18:12 -07:00
Self { thread_hdls }
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(())
}
}