From 3893126cc824722ba5c41bdd43caccec824f4856 Mon Sep 17 00:00:00 2001 From: Artem Medvedev Date: Sun, 7 Jul 2024 16:29:35 +0200 Subject: [PATCH] perf: avoid spawning log producer without consumers --- .../src/core/containers/async_container.rs | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/testcontainers/src/core/containers/async_container.rs b/testcontainers/src/core/containers/async_container.rs index 304c632a..44e2417a 100644 --- a/testcontainers/src/core/containers/async_container.rs +++ b/testcontainers/src/core/containers/async_container.rs @@ -65,23 +65,27 @@ where dropped: false, }; - let mut logs = container.docker_client.logs(&container.id, true); - let container_id = container.id.clone(); - tokio::spawn(async move { - while let Some(result) = logs.next().await { - match result { - Ok(record) => { - for consumer in &log_consumers { - consumer.accept(&record).await; - tokio::task::yield_now().await; + if !log_consumers.is_empty() { + let mut logs = container.docker_client.logs(&container.id, true); + let container_id = container.id.clone(); + tokio::spawn(async move { + while let Some(result) = logs.next().await { + match result { + Ok(record) => { + for consumer in &log_consumers { + consumer.accept(&record).await; + tokio::task::yield_now().await; + } + } + Err(err) => { + log::warn!( + "Failed to read log frame for container {container_id}: {err}", + ); } - } - Err(err) => { - log::warn!("Failed to read log frame for container {container_id}: {err}",); } } - } - }); + }); + } let ready_conditions = container.image().ready_conditions(); container.block_until_ready(ready_conditions).await?;