Skip to content

Commit 79d54ba

Browse files
committed
containerd-shim-spin: get_supported_triggers returns a HashSet instead of Vec
Signed-off-by: jiaxiao zhou <[email protected]>
1 parent 12fb2d5 commit 79d54ba

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

containerd-shim-spin/src/engine.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
collections::hash_map::DefaultHasher,
2+
collections::{hash_map::DefaultHasher, HashSet},
33
env,
44
hash::{Hash, Hasher},
55
};
@@ -137,28 +137,24 @@ impl SpinEngine {
137137
configure_application_variables_from_environment_variables(&locked_app)?;
138138
let trigger_cmds = get_supported_triggers(&locked_app)
139139
.with_context(|| format!("Couldn't find trigger executor for {app_source:?}"))?;
140-
141140
let _telemetry_guard = spin_telemetry::init(version!().to_string())?;
142141

143-
self.run_trigger(
144-
ctx,
145-
trigger_cmds.iter().map(|s| s.as_ref()).collect(),
146-
locked_app,
147-
app_source,
148-
)
149-
.await
142+
self.run_trigger(ctx, &trigger_cmds, locked_app, app_source)
143+
.await
150144
}
151145

152146
async fn run_trigger(
153147
&self,
154148
ctx: &impl RuntimeContext,
155-
trigger_types: Vec<&str>,
149+
trigger_types: &HashSet<String>,
156150
app: LockedApp,
157151
app_source: Source,
158152
) -> Result<()> {
159-
let mut futures_list = Vec::with_capacity(trigger_types.len());
153+
let mut futures_list = Vec::new();
154+
let mut trigger_type_map = Vec::new();
155+
160156
for trigger_type in trigger_types.iter() {
161-
let f = match trigger_type.to_owned() {
157+
let f = match trigger_type.as_str() {
162158
HttpTrigger::TRIGGER_TYPE => {
163159
let http_trigger =
164160
build_trigger::<HttpTrigger>(app.clone(), app_source.clone()).await?;
@@ -204,17 +200,17 @@ impl SpinEngine {
204200
}
205201
};
206202

207-
futures_list.push(f)
203+
trigger_type_map.push(trigger_type.clone());
204+
futures_list.push(f);
208205
}
209206

210207
info!(" >>> notifying main thread we are about to start");
211208

212209
// exit as soon as any of the trigger completes/exits
213210
let (result, index, rest) = future::select_all(futures_list).await;
214-
info!(
215-
" >>> trigger type '{trigger_type}' exited",
216-
trigger_type = trigger_types[index]
217-
);
211+
let trigger_type = &trigger_type_map[index];
212+
213+
info!(" >>> trigger type '{trigger_type}' exited");
218214

219215
drop(rest);
220216

containerd-shim-spin/src/trigger.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ async fn write_locked_app(locked_app: &LockedApp, working_dir: &Path) -> Result<
9191
/// - sqs
9292
/// - mqtt
9393
/// - command
94-
pub(crate) fn get_supported_triggers(locked_app: &LockedApp) -> anyhow::Result<Vec<String>> {
94+
///
95+
/// Note: this function returns a `HashSet` of supported trigger types. Duplicates are removed.
96+
pub(crate) fn get_supported_triggers(locked_app: &LockedApp) -> anyhow::Result<HashSet<String>> {
9597
let supported_triggers: HashSet<&str> = HashSet::from([
9698
RedisTrigger::TRIGGER_TYPE,
9799
HttpTrigger::TRIGGER_TYPE,
@@ -100,17 +102,17 @@ pub(crate) fn get_supported_triggers(locked_app: &LockedApp) -> anyhow::Result<V
100102
CommandTrigger::TRIGGER_TYPE,
101103
]);
102104

103-
let mut types: Vec<String> = Vec::with_capacity(locked_app.triggers.len());
104-
105-
for trigger in &locked_app.triggers {
106-
let trigger_type = &trigger.trigger_type;
107-
if !supported_triggers.contains(trigger_type.as_str()) {
108-
anyhow::bail!(
109-
"Only Http, Redis, MQTT, SQS, and Command triggers are currently supported. Found unsupported trigger: {:?}",
110-
trigger_type
111-
);
112-
}
113-
types.push(trigger_type.clone());
114-
}
115-
Ok(types)
105+
locked_app.triggers.iter()
106+
.map(|trigger| {
107+
let trigger_type = &trigger.trigger_type;
108+
if !supported_triggers.contains(trigger_type.as_str()) {
109+
Err(anyhow!(
110+
"Only Http, Redis, MQTT, SQS, and Command triggers are currently supported. Found unsupported trigger: {:?}",
111+
trigger_type
112+
))
113+
} else {
114+
Ok(trigger_type.clone())
115+
}
116+
})
117+
.collect::<Result<HashSet<_>>>()
116118
}

0 commit comments

Comments
 (0)