Skip to content

Commit 9adef9a

Browse files
committed
blueprint_loader: Remove internal last field
Tiny cleanup opportunity I noticed while implementing #9496 - we don't need to store a separate "most recently loaded" field (and update it in sync with our watch channel) because we can _just look in the watch channel_.
1 parent 9d7e1da commit 9adef9a

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

nexus/src/app/background/tasks/blueprint_load.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub struct LoadedTargetBlueprint {
2525

2626
pub struct TargetBlueprintLoader {
2727
datastore: Arc<DataStore>,
28-
last: Option<LoadedTargetBlueprint>,
2928
tx: watch::Sender<Option<LoadedTargetBlueprint>>,
3029
}
3130

@@ -34,7 +33,7 @@ impl TargetBlueprintLoader {
3433
datastore: Arc<DataStore>,
3534
tx: watch::Sender<Option<LoadedTargetBlueprint>>,
3635
) -> TargetBlueprintLoader {
37-
TargetBlueprintLoader { datastore, last: None, tx }
36+
TargetBlueprintLoader { datastore, tx }
3837
}
3938

4039
/// Expose the target blueprint
@@ -48,10 +47,14 @@ impl BackgroundTask for TargetBlueprintLoader {
4847
&'a mut self,
4948
opctx: &'a OpContext,
5049
) -> BoxFuture<'a, serde_json::Value> {
50+
// Clone the most-recently-loaded blueprint (if any), so we can check
51+
// whether the current target is different.
52+
let last = self.tx.borrow().clone();
53+
5154
async {
5255
// Set up a logger for this activation that includes metadata about
5356
// the current target.
54-
let log = match &self.last {
57+
let log = match &last {
5558
None => opctx.log.clone(),
5659
Some(LoadedTargetBlueprint { blueprint, .. }) => {
5760
opctx.log.new(o!(
@@ -91,7 +94,7 @@ impl BackgroundTask for TargetBlueprintLoader {
9194
let Some(LoadedTargetBlueprint {
9295
target: old_bp_target,
9396
blueprint: old_blueprint,
94-
}) = self.last.as_ref()
97+
}) = last
9598
else {
9699
// We've found a target blueprint for the first time.
97100
// Save it and notify any watchers.
@@ -103,11 +106,10 @@ impl BackgroundTask for TargetBlueprintLoader {
103106
"target_id" => %target_id,
104107
"time_created" => %time_created
105108
);
106-
self.last = Some(LoadedTargetBlueprint {
109+
self.tx.send_replace(Some(LoadedTargetBlueprint {
107110
target: new_bp_target,
108111
blueprint: Arc::new(new_blueprint),
109-
});
110-
self.tx.send_replace(self.last.clone());
112+
}));
111113
return json!({
112114
"target_id": target_id,
113115
"time_created": time_created,
@@ -127,11 +129,10 @@ impl BackgroundTask for TargetBlueprintLoader {
127129
"target_id" => %target_id,
128130
"time_created" => %time_created
129131
);
130-
self.last = Some(LoadedTargetBlueprint {
132+
self.tx.send_replace(Some(LoadedTargetBlueprint {
131133
target: new_bp_target,
132134
blueprint: Arc::new(new_blueprint),
133-
});
134-
self.tx.send_replace(self.last.clone());
135+
}));
135136
json!({
136137
"target_id": target_id,
137138
"time_created": time_created,
@@ -146,7 +147,7 @@ impl BackgroundTask for TargetBlueprintLoader {
146147
// It should not be possible for the contents of a
147148
// blueprint to change, but we check to catch possible
148149
// bugs further up the stack.
149-
if **old_blueprint != new_blueprint {
150+
if *old_blueprint != new_blueprint {
150151
let message = format!(
151152
"blueprint for id {} changed. Blueprints are supposed \
152153
to be immutable.",
@@ -173,11 +174,10 @@ impl BackgroundTask for TargetBlueprintLoader {
173174
"time_created" => %time_created,
174175
"state" => status,
175176
);
176-
self.last = Some(LoadedTargetBlueprint {
177+
self.tx.send_replace(Some(LoadedTargetBlueprint {
177178
target: new_bp_target,
178179
blueprint: Arc::new(new_blueprint),
179-
});
180-
self.tx.send_replace(self.last.clone());
180+
}));
181181
json!({
182182
"target_id": target_id,
183183
"time_created": time_created,

0 commit comments

Comments
 (0)