Skip to content

Commit 47533b4

Browse files
authored
Earlier exit for sim thread (#917)
## πŸ“ Summary This changes makes the sim thread finish as soon as the block ends. ## πŸ’‘ Motivation and Context Saw on a test version lots of sims happening after the block ended. ## βœ… I have completed the following steps: * [X] Run `make lint` * [X] Run `make test` * [ ] Added tests (if applicable)
1 parent fb94a25 commit 47533b4

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

β€Žcrates/rbuilder/src/live_builder/simulation/mod.rsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct SimulationContext {
3838
/// Simulation requests come in through this channel.
3939
pub requests: flume::Receiver<CancellableSimulationRequest>,
4040
/// Simulation results go out through this channel.
41+
/// This is also implicitly used as a cancellation token. If this is closed there is no need to simulate anymore.
4142
pub results: mpsc::Sender<SimulatedResult>,
4243
}
4344

β€Žcrates/rbuilder/src/live_builder/simulation/sim_worker.rsβ€Ž

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ pub fn run_sim_worker<P>(
5858
}
5959
};
6060
while let Ok(cancellable_task) = current_sim_context.requests.recv() {
61+
// Avoid starting sims when the output channel is closed.
62+
if current_sim_context.results.is_closed() {
63+
break;
64+
}
6165
if let Some(task) = cancellable_task.into_request() {
6266
let sim_thread_wait_time = last_sim_finished.elapsed();
6367
let sim_start = Instant::now();
@@ -86,10 +90,15 @@ pub fn run_sim_worker<P>(
8690
.collect(),
8791
simulation_time: start_time.elapsed(),
8892
};
89-
current_sim_context
90-
.results
91-
.try_send(result)
92-
.unwrap_or_default();
93+
if let Err(tokio::sync::mpsc::error::TrySendError::Full(_)) =
94+
current_sim_context.results.try_send(result)
95+
{
96+
error!(
97+
?order_id,
98+
"Simulation results channel is full, order dropped"
99+
);
100+
}
101+
93102
true
94103
}
95104
OrderSimResult::Failed(_) => false,

0 commit comments

Comments
Β (0)