Skip to content

Commit 7f23a0d

Browse files
committed
Shutting down the simulation if all the producers finish before the timeout.
1 parent 6528aee commit 7f23a0d

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

sim-lib/src/lib.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ pub struct ActivityParser {
139139
/// The destination of the payment.
140140
#[serde(with = "serializers::serde_node_id")]
141141
pub destination: NodeId,
142-
/// The time in the simulation to start the payment
142+
/// The time in the simulation to start the payment.
143143
#[serde(default)]
144144
pub start_secs: u16,
145-
/// The number of payments to send over the course of the simulation
145+
/// The number of payments to send over the course of the simulation.
146146
#[serde(default)]
147147
pub count: Option<u64>,
148148
/// The interval of the event, as in every how many seconds the payment is performed.
@@ -159,9 +159,9 @@ pub struct ActivityDefinition {
159159
pub source: NodeInfo,
160160
/// The destination of the payment.
161161
pub destination: NodeInfo,
162-
/// The time in the simulation to start the payment
162+
/// The time in the simulation to start the payment.
163163
pub start_secs: u16,
164-
/// The number of payments to send over the course of the simulation
164+
/// The number of payments to send over the course of the simulation.
165165
pub count: Option<u64>,
166166
/// The interval of the event, as in every how many seconds the payment is performed.
167167
pub interval_secs: u16,
@@ -570,9 +570,25 @@ impl Simulation {
570570
);
571571

572572
// Next, we'll spin up our actual producers that will be responsible for triggering the configured activity.
573-
self.dispatch_producers(activities, consumer_channels, &mut tasks)
573+
// The producers will use their own JoinSet so that the simulation can be shutdown if they all finish.
574+
let mut producer_tasks = JoinSet::new();
575+
self.dispatch_producers(activities, consumer_channels, &mut producer_tasks)
574576
.await?;
575577

578+
// Start a task that waits for the producers to finish.
579+
// If all producers finish, then there is nothing left to do and the simulation can be shutdown.
580+
let producer_trigger = self.shutdown_trigger.clone();
581+
tasks.spawn(async move {
582+
while let Some(res) = producer_tasks.join_next().await {
583+
if let Err(e) = res {
584+
log::error!("Producer exited with error: {e}.");
585+
}
586+
}
587+
log::info!("All producers finished. Shutting down.");
588+
producer_trigger.trigger()
589+
});
590+
591+
// Start a task that will shutdown the simulation if the total_time is met.
576592
if let Some(total_time) = self.total_time {
577593
let t = self.shutdown_trigger.clone();
578594
let l = self.shutdown_listener.clone();
@@ -948,6 +964,10 @@ async fn produce_events<N: DestinationGenerator + ?Sized, A: PaymentGenerator +
948964
}
949965

950966
let wait: Duration = if current_count == 0 {
967+
log::debug!(
968+
"Using a start delay. The first payment for {source} will be at {:?} seconds.",
969+
node_generator.payment_start()
970+
);
951971
node_generator.payment_start()
952972
} else {
953973
node_generator.next_payment_wait()

sim-lib/src/random_activity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ fn events_per_month(source_capacity_msat: u64, multiplier: f64, expected_payment
194194
}
195195

196196
impl PaymentGenerator for RandomPaymentActivity {
197-
/// Returns the time that the payments should start
197+
/// Returns the time that the payments should start. This will always be 0 for the RandomPaymentActivity type.
198198
fn payment_start(&self) -> Duration {
199199
Duration::from_secs(0)
200200
}
201201

202-
/// Returns the number of payments that should be made
202+
/// Returns the number of payments that should be made. This will always be None for the RandomPaymentActivity type.
203203
fn payment_count(&self) -> Option<u64> {
204204
None
205205
}

0 commit comments

Comments
 (0)