Skip to content

Commit

Permalink
Override event thread startup wait time from environment
Browse files Browse the repository at this point in the history
Add `HAB_EVENT_THREAD_STARTUP_WAIT_SEC` to your environment and have
fun!

Because hard-coded magic numbers suuuuuuuuuuuuuuck!

Signed-off-by: Christopher Maier <cmaier@chef.io>
  • Loading branch information
christophermaier committed May 2, 2019
1 parent 40f44cc commit 69ce79a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
28 changes: 28 additions & 0 deletions components/sup/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ pub use error::{Error,
use futures::sync::mpsc::UnboundedSender;
use habitat_common::types::{AutomateAuthToken,
EventStreamMetadata};
use habitat_core::env::Config as EnvConfig;
use state::Container;
use std::{net::SocketAddr,
num::ParseIntError,
str::FromStr,
sync::Once,
time::Duration};

Expand Down Expand Up @@ -254,3 +257,28 @@ impl EventStream {
}
}
}

////////////////////////////////////////////////////////////////////////

/// How long should we for the event thread to start up before
/// abandoning it and shutting down?
#[derive(Clone, Debug)]
struct EventThreadStartupWait(u64);

impl Default for EventThreadStartupWait {
fn default() -> Self { Self(5) }
}

impl FromStr for EventThreadStartupWait {
type Err = ParseIntError;

fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> { Ok(Self(s.parse()?)) }
}

impl EnvConfig for EventThreadStartupWait {
const ENVVAR: &'static str = "HAB_EVENT_THREAD_STARTUP_WAIT_SEC";
}

impl Into<Duration> for EventThreadStartupWait {
fn into(self) -> Duration { Duration::from_secs(self.0) }
}
7 changes: 4 additions & 3 deletions components/sup/src/event/nitox.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use super::EventThreadStartupWait;
use crate::event::{Error,
EventConnectionInfo,
EventStream,
Result};
use futures::{sync::mpsc as futures_mpsc,
Future,
Stream};
use habitat_core::env::Config as _;
use nitox::{commands::ConnectCommand,
streaming::client::NatsStreamingClient,
NatsClient,
NatsClientOptions};
use std::{sync::mpsc as std_mpsc,
thread,
time::Duration};
thread};
use tokio::{executor,
runtime::current_thread::Runtime as ThreadRuntime};
/// All messages are published under this subject.
Expand Down Expand Up @@ -83,7 +84,7 @@ pub(super) fn init_stream(conn_info: EventConnectionInfo) -> Result<EventStream>
})
.map_err(Error::SpawnEventThreadError)?;

sync_rx.recv_timeout(Duration::from_secs(5))
sync_rx.recv_timeout(EventThreadStartupWait::configured_value().into())
.map_err(Error::ConnectEventServerError)?;
Ok(EventStream(event_tx))
}
7 changes: 4 additions & 3 deletions components/sup/src/event/ratsio.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use super::EventThreadStartupWait;
use crate::event::{Error,
EventConnectionInfo,
EventStream,
Result};
use futures::{sync::mpsc as futures_mpsc,
Future,
Stream};
use habitat_core::env::Config as _;
use ratsio::{nats_client::NatsClientOptions,
stan_client::{StanClient,
StanMessage,
StanOptions}};
use std::{sync::mpsc as std_mpsc,
thread,
time::Duration};
thread};
use tokio::{executor,
runtime::current_thread::Runtime as ThreadRuntime};

Expand Down Expand Up @@ -78,7 +79,7 @@ pub(super) fn init_stream(conn_info: EventConnectionInfo) -> Result<EventStream>
})
.map_err(Error::SpawnEventThreadError)?; // TODO (CM): ratsio error variant

sync_rx.recv_timeout(Duration::from_secs(5))
sync_rx.recv_timeout(EventThreadStartupWait::configured_value().into())
.map_err(Error::ConnectEventServerError)?;
Ok(EventStream(event_tx))
}

0 comments on commit 69ce79a

Please sign in to comment.