Skip to content

Commit

Permalink
containerd-shim-spin: get_supported_triggers returns a HashSet instea…
Browse files Browse the repository at this point in the history
…d of Vec

Signed-off-by: jiaxiao zhou <duibao55328@gmail.com>
  • Loading branch information
Mossaka committed Sep 19, 2024
1 parent 12fb2d5 commit 79d54ba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
30 changes: 13 additions & 17 deletions containerd-shim-spin/src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::hash_map::DefaultHasher,
collections::{hash_map::DefaultHasher, HashSet},
env,
hash::{Hash, Hasher},
};
Expand Down Expand Up @@ -137,28 +137,24 @@ impl SpinEngine {
configure_application_variables_from_environment_variables(&locked_app)?;
let trigger_cmds = get_supported_triggers(&locked_app)
.with_context(|| format!("Couldn't find trigger executor for {app_source:?}"))?;

let _telemetry_guard = spin_telemetry::init(version!().to_string())?;

self.run_trigger(
ctx,
trigger_cmds.iter().map(|s| s.as_ref()).collect(),
locked_app,
app_source,
)
.await
self.run_trigger(ctx, &trigger_cmds, locked_app, app_source)
.await
}

async fn run_trigger(
&self,
ctx: &impl RuntimeContext,
trigger_types: Vec<&str>,
trigger_types: &HashSet<String>,
app: LockedApp,
app_source: Source,
) -> Result<()> {
let mut futures_list = Vec::with_capacity(trigger_types.len());
let mut futures_list = Vec::new();
let mut trigger_type_map = Vec::new();

for trigger_type in trigger_types.iter() {
let f = match trigger_type.to_owned() {
let f = match trigger_type.as_str() {
HttpTrigger::TRIGGER_TYPE => {
let http_trigger =
build_trigger::<HttpTrigger>(app.clone(), app_source.clone()).await?;
Expand Down Expand Up @@ -204,17 +200,17 @@ impl SpinEngine {
}
};

futures_list.push(f)
trigger_type_map.push(trigger_type.clone());
futures_list.push(f);
}

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

// exit as soon as any of the trigger completes/exits
let (result, index, rest) = future::select_all(futures_list).await;
info!(
" >>> trigger type '{trigger_type}' exited",
trigger_type = trigger_types[index]
);
let trigger_type = &trigger_type_map[index];

info!(" >>> trigger type '{trigger_type}' exited");

drop(rest);

Expand Down
30 changes: 16 additions & 14 deletions containerd-shim-spin/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ async fn write_locked_app(locked_app: &LockedApp, working_dir: &Path) -> Result<
/// - sqs
/// - mqtt
/// - command
pub(crate) fn get_supported_triggers(locked_app: &LockedApp) -> anyhow::Result<Vec<String>> {
///
/// Note: this function returns a `HashSet` of supported trigger types. Duplicates are removed.
pub(crate) fn get_supported_triggers(locked_app: &LockedApp) -> anyhow::Result<HashSet<String>> {
let supported_triggers: HashSet<&str> = HashSet::from([
RedisTrigger::TRIGGER_TYPE,
HttpTrigger::TRIGGER_TYPE,
Expand All @@ -100,17 +102,17 @@ pub(crate) fn get_supported_triggers(locked_app: &LockedApp) -> anyhow::Result<V
CommandTrigger::TRIGGER_TYPE,
]);

let mut types: Vec<String> = Vec::with_capacity(locked_app.triggers.len());

for trigger in &locked_app.triggers {
let trigger_type = &trigger.trigger_type;
if !supported_triggers.contains(trigger_type.as_str()) {
anyhow::bail!(
"Only Http, Redis, MQTT, SQS, and Command triggers are currently supported. Found unsupported trigger: {:?}",
trigger_type
);
}
types.push(trigger_type.clone());
}
Ok(types)
locked_app.triggers.iter()
.map(|trigger| {
let trigger_type = &trigger.trigger_type;
if !supported_triggers.contains(trigger_type.as_str()) {
Err(anyhow!(
"Only Http, Redis, MQTT, SQS, and Command triggers are currently supported. Found unsupported trigger: {:?}",
trigger_type
))
} else {
Ok(trigger_type.clone())
}
})
.collect::<Result<HashSet<_>>>()
}

0 comments on commit 79d54ba

Please sign in to comment.