Fix race for nomos log service (#577)

This commit is contained in:
Al Liu 2024-03-23 22:02:01 +08:00 committed by GitHub
parent b341a11eb2
commit 16444cf32a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 0 deletions

View File

@ -144,6 +144,11 @@ macro_rules! registry_init {
#[async_trait::async_trait]
impl ServiceCore for Logger {
fn init(service_state: ServiceStateHandle<Self>) -> Result<Self, overwatch_rs::DynError> {
#[cfg(test)]
use std::sync::Once;
#[cfg(test)]
static ONCE_INIT: Once = Once::new();
let config = service_state.settings_reader.get_updated_settings();
let (non_blocking, _guard) = match config.backend {
LoggerBackend::Gelf { addr } => {
@ -152,7 +157,13 @@ impl ServiceCore for Logger {
.overwatch_handle
.runtime()
.spawn(async move { task.connect().await });
#[cfg(test)]
ONCE_INIT.call_once(move || {
registry_init!(layer, config.format, config.level);
});
#[cfg(not(test))]
registry_init!(layer, config.format, config.level);
return Ok(Self {
service_state,
worker_guard: None,
@ -179,7 +190,13 @@ impl ServiceCore for Logger {
let layer = tracing_subscriber::fmt::Layer::new()
.with_level(true)
.with_writer(non_blocking);
#[cfg(test)]
ONCE_INIT.call_once(move || {
registry_init!(layer, config.format, config.level);
});
#[cfg(not(test))]
registry_init!(layer, config.format, config.level);
Ok(Self {
service_state,
worker_guard: Some(_guard),