solana/core/src/fetch_stage.rs

68 lines
2.0 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>,
tpu_via_blobs_sockets: Vec<UdpSocket>,
exit: &Arc<AtomicBool>,
) -> (Self, PacketReceiver) {
let (sender, receiver) = channel();
(
Self::new_with_sender(sockets, tpu_via_blobs_sockets, exit, &sender),
receiver,
)
}
pub fn new_with_sender(
sockets: Vec<UdpSocket>,
tpu_via_blobs_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();
let tpu_via_blobs_sockets = tpu_via_blobs_sockets.into_iter().map(Arc::new).collect();
Self::new_multi_socket(tx_sockets, tpu_via_blobs_sockets, exit, &sender)
}
fn new_multi_socket(
sockets: Vec<Arc<UdpSocket>>,
tpu_via_blobs_sockets: Vec<Arc<UdpSocket>>,
2019-03-04 19:53:50 -08:00
exit: &Arc<AtomicBool>,
sender: &PacketSender,
) -> Self {
let tpu_threads = sockets
.into_iter()
.map(|socket| streamer::receiver(socket, &exit, sender.clone(), "fetch-stage"));
let tpu_via_blobs_threads = tpu_via_blobs_sockets
.into_iter()
.map(|socket| streamer::blob_packet_receiver(socket, &exit, sender.clone()));
2018-05-29 10:18:12 -07:00
let thread_hdls: Vec<_> = tpu_threads.chain(tpu_via_blobs_threads).collect();
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(())
}
}