Skip to content

Commit

Permalink
Eager command line argument validation for wasmtime serve
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottt committed Oct 3, 2023
1 parent cda8240 commit 174b010
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ pub struct ServeCommand {
impl ServeCommand {
/// Start a server to run the given wasi-http proxy component
pub fn execute(self) -> Result<()> {
// We force cli errors before starting to listen for connections so tha we don't
// accidentally delay them to the first request.
if self.run.common.wasi.nn == Some(true) {
#[cfg(not(feature = "wasi-nn"))]
{
bail!("Cannot enable wasi-nn when the binary is not compiled with this feature.");
}
}

if let Some(Profile::Guest { .. }) = &self.run.profile {
bail!("Cannot use the guest profiler with components");
}

if self.run.common.wasi.nn == Some(true) {
#[cfg(not(feature = "wasi-nn"))]
{
bail!("Cannot enable wasi-nn when the binary is not compiled with this feature.");
}
}

if self.run.common.wasi.threads == Some(true) {
bail!("wasi-threads does not support components yet")
}

let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_time()
.enable_io()
Expand Down Expand Up @@ -110,10 +134,6 @@ impl ServeCommand {
};

if self.run.common.wasi.nn == Some(true) {
#[cfg(not(feature = "wasi-nn"))]
{
bail!("Cannot enable wasi-nn when the binary is not compiled with this feature.");
}
#[cfg(feature = "wasi-nn")]
{
let graphs = self
Expand All @@ -131,17 +151,19 @@ impl ServeCommand {

let mut store = Store::new(engine, host);

if let Some(Profile::Guest { .. }) = &self.run.profile {
bail!("Cannot use the guest profiler with components");
}

if self.run.common.wasm.timeout.is_some() {
store.set_epoch_deadline(1);
}

store.data_mut().limits = self.run.store_limits();
store.limiter(|t| &mut t.limits);

// If fuel has been configured, we want to add the configured
// fuel amount to this store.
if let Some(fuel) = self.run.common.wasm.fuel {
store.add_fuel(fuel)?;
}

Ok(store)
}

Expand All @@ -153,20 +175,12 @@ impl ServeCommand {
wasmtime_wasi_http::proxy::add_to_linker(linker)?;

if self.run.common.wasi.nn == Some(true) {
#[cfg(not(feature = "wasi-nn"))]
{
bail!("Cannot enable wasi-nn when the binary is not compiled with this feature.");
}
#[cfg(feature = "wasi-nn")]
{
wasmtime_wasi_nn::wit::ML::add_to_linker(linker, |host| host.nn.as_mut().unwrap())?;
}
}

if self.run.common.wasi.threads == Some(true) {
bail!("wasi-threads does not support components yet")
}

Ok(())
}

Expand All @@ -187,9 +201,10 @@ impl ServeCommand {
Some(Profile::Native(s)) => {
config.profiler(s);
}
Some(Profile::Guest { .. }) => {
bail!("guest profiling not yet available with components");
}

// We bail early in `execute` if the guest profiler is configured.
Some(Profile::Guest { .. }) => unreachable!(),

None => {}
}

Expand Down

0 comments on commit 174b010

Please sign in to comment.