Skip to content

Commit

Permalink
parametrize maximum wait time for child process
Browse files Browse the repository at this point in the history
  • Loading branch information
kobkaz committed Apr 25, 2024
1 parent 71ba574 commit 00a923c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 12 additions & 6 deletions kble/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct Connections<'a> {
// Some: connections not used yet
// None: connections is used in a link
map: HashMap<&'a str, Connection>,
max_child_wait_secs: u64,
}

struct Link<'a> {
Expand All @@ -27,8 +28,8 @@ struct Link<'a> {
dest: plug::PlugSink,
}

pub async fn run(config: &Config) -> Result<()> {
let mut conns = connect_to_plugs(config).await?;
pub async fn run(config: &Config, max_child_wait_secs: u64) -> Result<()> {
let mut conns = connect_to_plugs(config, max_child_wait_secs).await?;
let links = connect_links(&mut conns, config);

let (quit_tx, _) = broadcast::channel(1);
Expand All @@ -52,9 +53,10 @@ pub async fn run(config: &Config) -> Result<()> {
}

impl<'a> Connections<'a> {
fn new() -> Self {
fn new(max_child_wait_secs: u64) -> Self {
Self {
map: HashMap::new(),
max_child_wait_secs,
}
}

Expand Down Expand Up @@ -108,7 +110,11 @@ impl<'a> Connections<'a> {
debug!("Plug {name} exited");
anyhow::Ok(())
};
let close_result = tokio::time::timeout(std::time::Duration::from_secs(10), fut).await;
let close_result = tokio::time::timeout(
std::time::Duration::from_secs(self.max_child_wait_secs),
fut,
)
.await;

match close_result {
Ok(result) => result,
Expand All @@ -132,8 +138,8 @@ impl<'a> Connections<'a> {
}
}

async fn connect_to_plugs(config: &Config) -> Result<Connections> {
let mut conns = Connections::new();
async fn connect_to_plugs(config: &Config, max_child_wait_secs: u64) -> Result<Connections> {
let mut conns = Connections::new(max_child_wait_secs);
for (name, url) in config.plugs().iter() {
debug!("Connecting to {name}");
let connect_result = plug::connect(url).await.with_context(move || {
Expand Down
6 changes: 5 additions & 1 deletion kble/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ use spaghetti::{Config, Raw};
struct Args {
#[clap(long, short)]
spaghetti: PathBuf,

/// Maximum time to wait for a child process to exit after a closing handshake
#[clap(long, default_value_t = 10)]
max_child_wait_secs: u64,
}

impl Args {
Expand Down Expand Up @@ -45,6 +49,6 @@ async fn main() -> Result<()> {

let args = Args::parse_with_license_notice(include_notice!());
let config = args.load_spaghetti_config()?;
app::run(&config).await?;
app::run(&config, args.max_child_wait_secs).await?;
Ok(())
}

0 comments on commit 00a923c

Please sign in to comment.