fix: Only enable tokio components for servers

Only enable the tokio and tracing components for server commands.
This commit is contained in:
teor 2020-07-17 07:39:10 +10:00
parent 49a3a7d6d1
commit 71de6de701
1 changed files with 29 additions and 17 deletions

View File

@ -84,11 +84,15 @@ impl Application for ZebradApp {
&mut self, &mut self,
command: &Self::Cmd, command: &Self::Cmd,
) -> Result<Vec<Box<dyn Component<Self>>>, FrameworkError> { ) -> Result<Vec<Box<dyn Component<Self>>>, FrameworkError> {
let terminal = Terminal::new(self.term_colors(command));
let tracing = self.tracing_component(command);
color_eyre::install().unwrap(); color_eyre::install().unwrap();
Ok(vec![Box::new(terminal), Box::new(tracing)]) let terminal = Terminal::new(self.term_colors(command));
if ZebradApp::command_is_server(&command) {
let tracing = self.tracing_component(command);
Ok(vec![Box::new(terminal), Box::new(tracing)])
} else {
Ok(vec![Box::new(terminal)])
}
} }
/// Register all components used by this application. /// Register all components used by this application.
@ -101,16 +105,10 @@ impl Application for ZebradApp {
metrics::MetricsEndpoint, tokio::TokioComponent, tracing::TracingEndpoint, metrics::MetricsEndpoint, tokio::TokioComponent, tracing::TracingEndpoint,
}; };
// `None` outputs zebrad usage information and exits
let command_is_server = match &command.command {
None => false,
Some(c) => c.is_server(),
};
let mut components = self.framework_components(command)?; let mut components = self.framework_components(command)?;
components.push(Box::new(TokioComponent::new()?));
// Launch network endpoints for long-running commands // Launch network endpoints for long-running commands
if command_is_server { if ZebradApp::command_is_server(&command) {
components.push(Box::new(TokioComponent::new()?));
components.push(Box::new(TracingEndpoint::new()?)); components.push(Box::new(TracingEndpoint::new()?));
components.push(Box::new(MetricsEndpoint::new()?)); components.push(Box::new(MetricsEndpoint::new()?));
} }
@ -132,12 +130,14 @@ impl Application for ZebradApp {
self.state.components.after_config(&config)?; self.state.components.after_config(&config)?;
self.config = Some(config); self.config = Some(config);
let level = self.level(command); if ZebradApp::command_is_server(&command) {
self.state let level = self.level(command);
.components self.state
.get_downcast_mut::<Tracing>() .components
.expect("Tracing component should be available") .get_downcast_mut::<Tracing>()
.reload_filter(level); .expect("Tracing component should be available")
.reload_filter(level);
}
Ok(()) Ok(())
} }
@ -198,4 +198,16 @@ impl ZebradApp {
filter_handle.into() filter_handle.into()
} }
/// Returns true if command is a server command.
///
/// Server commands use long-running components such as tracing, metrics,
/// and the tokio runtime.
fn command_is_server(command: &EntryPoint<ZebradCmd>) -> bool {
// `None` outputs zebrad usage information and exits
match &command.command {
None => false,
Some(c) => c.is_server(),
}
}
} }