Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce 'idle' generator #706

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lading/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod file_gen;
pub mod file_tree;
pub mod grpc;
pub mod http;
pub mod idle;
pub mod process_tree;
pub mod splunk_hec;
pub mod tcp;
Expand Down Expand Up @@ -59,6 +60,9 @@ pub enum Error {
/// See [`crate::generator::process_tree::Error`] for details.
#[error(transparent)]
ProcessTree(#[from] process_tree::Error),
/// See [`crate::generator::idle::Error`] for details.
#[error(transparent)]
Idle(#[from] idle::Error),
}

#[derive(Debug, Deserialize, PartialEq)]
Expand Down Expand Up @@ -105,6 +109,8 @@ pub enum Inner {
UnixDatagram(unix_datagram::Config),
/// See [`crate::generator::process_tree::Config`] for details.
ProcessTree(process_tree::Config),
/// See [`crate::generator::idle::Config`] for details.
Idle(idle::Config),
}

#[derive(Debug)]
Expand Down Expand Up @@ -133,6 +139,8 @@ pub enum Server {
UnixDatagram(unix_datagram::UnixDatagram),
/// See [`crate::generator::process_tree::ProcessTree`] for details.
ProcessTree(process_tree::ProcessTree),
/// See [`crate::generator::idle::Idle`] for details.
Idle(idle::Idle),
}

impl Server {
Expand Down Expand Up @@ -171,6 +179,7 @@ impl Server {
Inner::ProcessTree(conf) => {
Self::ProcessTree(process_tree::ProcessTree::new(&conf, shutdown)?)
}
Inner::Idle(conf) => Self::Idle(idle::Idle::new(&conf, shutdown)?),
};
Ok(srv)
}
Expand Down Expand Up @@ -202,6 +211,7 @@ impl Server {
Server::UnixStream(inner) => inner.spin().await?,
Server::UnixDatagram(inner) => inner.spin().await?,
Server::ProcessTree(inner) => inner.spin().await?,
Server::Idle(inner) => inner.spin().await?,
};

Ok(())
Expand Down
64 changes: 64 additions & 0 deletions lading/src/generator/idle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! The UDP protocol speaking generator.
//!
//! ## Metrics
//!
//! `bytes_written`: Bytes written successfully
//! `packets_sent`: Packets written successfully
//! `request_failure`: Number of failed writes; each occurrence causes a socket re-bind
//! `connection_failure`: Number of socket bind failures
//! `bytes_per_second`: Configured rate to send data
//!
//! Additional metrics may be emitted by this generator's [throttle].
//!

use serde::Deserialize;
use tracing::info;

use crate::signals::Shutdown;

#[derive(Debug, Deserialize, PartialEq, Clone, Copy)]
/// Configuration of this generator.
pub struct Config {}

/// Errors produced by [`Idle`].
#[derive(thiserror::Error, Debug, Clone, Copy)]
pub enum Error {}

#[derive(Debug)]
/// The Idle generator.
///
/// This generator is responsible for doing nothing, except wait to shut down.
pub struct Idle {
shutdown: Shutdown,
}

impl Idle {
/// Create a new [`Idle`] instance
///
/// # Errors
///
/// Creation will not fail.
///
/// # Panics
///
/// Function will not panic.
#[allow(clippy::cast_possible_truncation)]
pub fn new(_config: &Config, shutdown: Shutdown) -> Result<Self, Error> {
Ok(Self { shutdown })
}

/// Run [`Idle`] to completion or until a shutdown signal is received.
///
/// # Errors
///
/// Function will return an error when the UDP socket cannot be written to.
///
/// # Panics
///
/// Function will panic if underlying byte capacity is not available.
pub async fn spin(mut self) -> Result<(), Error> {
self.shutdown.recv().await;
info!("shutdown signal received");
Ok(())
}
}