Skip to content

Commit

Permalink
sim-lib/refactor: pass report logger into results method
Browse files Browse the repository at this point in the history
We're going to need to start the report logger in its own task in the
commit that follows (so that we can log every minute). This preparatory
commit refactors creations of a PaymentResultLogger so that we can
easily spin up its run task in our existing join set.
  • Loading branch information
carlaKC committed Oct 24, 2023
1 parent 3001a22 commit 2086546
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,10 @@ impl Simulation {
listener.clone(),
));

let result_logger = Arc::new(Mutex::new(PaymentResultLogger::new()));

tasks.spawn(consume_simulation_results(
result_logger,
results_receiver,
listener,
self.print_batch_size,
Expand Down Expand Up @@ -865,21 +868,25 @@ async fn produce_random_events<N: NetworkGenerator, A: PaymentGenerator + Displa
}

async fn consume_simulation_results(
logger: Arc<Mutex<PaymentResultLogger>>,
receiver: Receiver<(Payment, PaymentResult)>,
listener: Listener,
print_batch_size: u32,
no_results: bool,
) {
log::debug!("Simulation results consumer started.");

if let Err(e) = write_payment_results(receiver, listener, print_batch_size, no_results).await {
if let Err(e) =
write_payment_results(logger, receiver, listener, print_batch_size, no_results).await
{
log::error!("Error while reporting payment results: {:?}.", e);
}

log::debug!("Simulation results consumer exiting.");
}

async fn write_payment_results(
logger: Arc<Mutex<PaymentResultLogger>>,
mut receiver: Receiver<(Payment, PaymentResult)>,
listener: Listener,
print_batch_size: u32,
Expand All @@ -897,8 +904,7 @@ async fn write_payment_results(
None
};

let mut result_logger = PaymentResultLogger::new();
let mut counter = 0;
let mut counter = 1;
loop {
tokio::select! {
biased;
Expand All @@ -909,7 +915,7 @@ async fn write_payment_results(
payment_report = receiver.recv() => {
match payment_report {
Some((details, result)) => {
result_logger.report_result(&details, &result);
logger.lock().await.report_result(&details, &result);
log::trace!("Resolved dispatched payment: {} with: {}.", details, result);

if let Some(ref mut w) = writer {
Expand Down

0 comments on commit 2086546

Please sign in to comment.