Skip to content

Commit

Permalink
Merge pull request #29 from G-Core/feat/dictionary
Browse files Browse the repository at this point in the history
Support for dictionary interface
  • Loading branch information
ruslanti authored Aug 21, 2024
2 parents d902384 + 8ce9f30 commit 3696435
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["crates/*"]
resolver = "2"

[workspace.package]
version = "0.5.2"
version = "0.5.3"
edition = "2021"
publish = false
authors = ["FastEdge Development Team"]
Expand Down Expand Up @@ -57,6 +57,7 @@ pretty_env_logger = "0.5"
runtime = { path = "crates/runtime", default-features = false }
http-service = { path = "crates/http-service" }
http-backend = { path = "crates/http-backend" }
dictionary = { path = "crates/dictionary" }
hyper-tls = "0.6"
hyper-util = { version = "0.1", features = ["client", "client-legacy", "http1", "tokio"] }
http-body-util = "0.1"
Expand Down
17 changes: 17 additions & 0 deletions crates/dictionary/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "dictionary"
version.workspace = true
edition.workspace = true
publish.workspace = true
authors.workspace = true
description = "dictionary host function"

[dependencies]
reactor = { path = "../reactor" }
anyhow = {workspace = true}
tracing = {workspace = true}
async-trait = {workspace = true}
wasmtime = {workspace = true}

[lints]
workspace = true
47 changes: 47 additions & 0 deletions crates/dictionary/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use async_trait::async_trait;
use reactor::gcore::fastedge::dictionary;

#[derive(Clone)]
pub struct Dictionary{
inner: HashMap<String, String>
}

#[async_trait]
impl dictionary::Host for Dictionary {
async fn get(&mut self, name: String) -> anyhow::Result<Option<String>> {
Ok(self.inner.get(&name).map(|v| v.to_string()))
}
}

impl Default for Dictionary {
fn default() -> Self {
Self{
inner: Default::default()
}
}
}

impl Dictionary {
pub fn new() -> Self {
Self{
inner: Default::default()
}
}
}

impl Deref for Dictionary {
type Target = HashMap<String, String>;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl DerefMut for Dictionary {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}

2 changes: 1 addition & 1 deletion crates/http-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ authors.workspace = true
default = []
metrics = ["runtime/metrics"]
stats = ["runtime/stats"]
#tls = ["tokio-rustls", "rustls-pemfile", "hyper-rustls", "rustls"]

[dependencies]
anyhow = { workspace = true }
Expand All @@ -26,6 +25,7 @@ smol_str = { workspace = true }
reactor = { path = "../reactor" }
runtime = { path = "../runtime" }
http-backend = { path = "../http-backend" }
dictionary = { path = "../dictionary" }
nanoid = "0.4"
bytesize = "1.3.0"
futures = "0.3.30"
Expand Down
6 changes: 5 additions & 1 deletion crates/http-service/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use runtime::store::StoreBuilder;
use runtime::{App, InstancePre, WasmEngine};
use smol_str::SmolStr;
use wasmtime_wasi::StdoutStream;

use dictionary::Dictionary;
use crate::state::HttpState;

pub use wasi_http::WasiHttpExecutorImpl;
Expand Down Expand Up @@ -52,6 +52,7 @@ pub struct HttpExecutorImpl<C> {
instance_pre: InstancePre<HttpState<C>>,
store_builder: StoreBuilder,
backend: Backend<C>,
dictionary: Dictionary
}

#[async_trait]
Expand Down Expand Up @@ -82,11 +83,13 @@ where
instance_pre: InstancePre<HttpState<C>>,
store_builder: StoreBuilder,
backend: Backend<C>,
dictionary: Dictionary,
) -> Self {
Self {
instance_pre,
store_builder,
backend,
dictionary,
}
}

Expand Down Expand Up @@ -148,6 +151,7 @@ where
uri: backend_uri,
propagate_headers: parts.headers,
propagate_header_names,
dictionary: self.dictionary.clone(),
};

let mut store = store_builder.build(state)?;
Expand Down
6 changes: 5 additions & 1 deletion crates/http-service/src/executor/wasi_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use hyper::body::{Body, Bytes};
use smol_str::ToSmolStr;
use tracing::error;
use wasmtime_wasi_http::{ WasiHttpView};

use dictionary::Dictionary;
use http_backend::Backend;
use runtime::store::StoreBuilder;
use runtime::InstancePre;
Expand All @@ -25,6 +25,7 @@ pub struct WasiHttpExecutorImpl<C> {
instance_pre: InstancePre<HttpState<C>>,
store_builder: StoreBuilder,
backend: Backend<C>,
dictionary: Dictionary
}

#[async_trait]
Expand Down Expand Up @@ -55,11 +56,13 @@ where
instance_pre: InstancePre<HttpState<C>>,
store_builder: StoreBuilder,
backend: Backend<C>,
dictionary: Dictionary,
) -> Self {
Self {
instance_pre,
store_builder,
backend,
dictionary,
}
}

Expand Down Expand Up @@ -131,6 +134,7 @@ where
uri: backend_uri,
propagate_headers,
propagate_header_names,
dictionary: self.dictionary.clone(),
};

let mut store = store_builder.build(state).context("store build")?;
Expand Down
15 changes: 13 additions & 2 deletions crates/http-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,18 @@ where
let linker = builder.component_linker_ref();
wasmtime_wasi_nn::wit::ML::add_to_linker(linker, |data| &mut data.as_mut().wasi_nn)?;
// Allow re-importing of `wasi:clocks/wall-clock@0.2.0`
wasmtime_wasi::add_to_linker_async(linker)?;
linker.allow_shadowing(true);
wasmtime_wasi_http::proxy::add_to_linker(linker)?;
wasmtime_wasi::add_to_linker_async(linker)?;

reactor::gcore::fastedge::http_client::add_to_linker(linker, |data| {
&mut data.as_mut().http_backend
})?;

reactor::gcore::fastedge::dictionary::add_to_linker(linker, |data| {
&mut data.as_mut().dictionary
})?;

Ok(())
}
}
Expand Down Expand Up @@ -525,7 +530,7 @@ mod tests {
use test_case::test_case;
use wasmtime::component::Component;
use wasmtime::{Engine, Module};

use dictionary::Dictionary;
use http_backend::{Backend, BackendStrategy, FastEdgeConnector};
use runtime::logger::{Logger, NullAppender};
use runtime::service::ServiceBuilder;
Expand Down Expand Up @@ -630,6 +635,11 @@ mod tests {
cfg: &App,
engine: &WasmEngine<HttpState<FastEdgeConnector>>,
) -> Result<Self::Executor> {
let mut dictionary = Dictionary::new();
for (k, v) in cfg.env.iter() {
dictionary.insert(k.to_string(), v.to_string());
}

let env = cfg.env.iter().collect::<Vec<(&SmolStr, &SmolStr)>>();

let logger = self.make_logger(name.clone(), cfg);
Expand All @@ -649,6 +659,7 @@ mod tests {
instance_pre,
store_builder,
self.backend(),
dictionary,
))
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/http-service/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use runtime::BackendRequest;
use smol_str::{SmolStr, ToSmolStr};
use tracing::instrument;
use wasmtime_wasi_nn::WasiNnCtx;
use dictionary::Dictionary;

pub struct HttpState<C> {
pub(super) wasi_nn: WasiNnCtx,
pub(super) http_backend: Backend<C>,
pub(super) uri: Uri,
pub(super) propagate_headers: HeaderMap,
pub(super) propagate_header_names: Vec<SmolStr>,
pub(super) dictionary: Dictionary
}

impl<C> BackendRequest for HttpState<C> {
Expand Down
5 changes: 5 additions & 0 deletions crates/runtime/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ impl StoreBuilder {
) -> Result<Store<T>> {
let table = ResourceTable::new();

debug!("ENV: {:?}", self.env);
wasi_ctx_builder.envs(&self.env);
debug!("WasiNnCtxBuilder: {:?}", self.version);
//wasi_ctx_builder.inherit_env();

let logger = if let Some(mut logger) = self.logger {
logger.extend(self.properties);
Expand All @@ -206,9 +209,11 @@ impl StoreBuilder {
} else {
if cfg!(debug_assertions) {
wasi_ctx_builder.inherit_stdout();
wasi_ctx_builder.inherit_stderr();
}
None
};

let wasi = match self.version {
WasiVersion::Preview1 => Wasi::Preview1(wasi_ctx_builder.build_p1()),
WasiVersion::Preview2 => Wasi::Preview2(wasi_ctx_builder.build()),
Expand Down
2 changes: 1 addition & 1 deletion sdk
Submodule sdk updated 5 files
+164 −105 Cargo.lock
+3 −3 Cargo.toml
+8 −1 src/lib.rs
+6 −0 wit/dictionary.wit
+2 −0 wit/world.wit
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_trait::async_trait;
use bytesize::ByteSize;
use bytesize::{ByteSize, MB};
use clap::{Args, Parser, Subcommand};
use http::{Request, Response};
use http_backend::{Backend, BackendStrategy};
Expand Down Expand Up @@ -28,6 +28,7 @@ use std::path::PathBuf;
use std::time::Duration;
use wasmtime::component::Component;
use wasmtime::{Engine, Module};
use dictionary::Dictionary;

#[derive(Debug, Parser)]
#[command(name = "cli")]
Expand Down Expand Up @@ -106,7 +107,7 @@ async fn main() -> anyhow::Result<()> {
let cli_app = App {
binary_id: 0,
max_duration: run.max_duration.map(|m| m / 10).unwrap_or(60000),
mem_limit: run.mem_limit.unwrap_or(u32::MAX as usize),
mem_limit: run.mem_limit.unwrap_or((128 * MB) as usize),
env: run.envs.unwrap_or_default().into_iter().collect(),
rsp_headers: Default::default(),
log: Default::default(),
Expand Down Expand Up @@ -235,6 +236,11 @@ impl ExecutorFactory<HttpState<HttpsConnector<HttpConnector>>> for CliContext {
app: &App,
engine: &WasmEngine<HttpState<HttpsConnector<HttpConnector>>>,
) -> anyhow::Result<Self::Executor> {
let mut dictionary = Dictionary::new();
for (k, v) in app.env.iter() {
dictionary.insert(k.to_string(), v.to_string());
}

let env = app.env.iter().collect::<Vec<(&SmolStr, &SmolStr)>>();

let logger = self.make_logger(name, app);
Expand All @@ -254,12 +260,14 @@ impl ExecutorFactory<HttpState<HttpsConnector<HttpConnector>>> for CliContext {
instance_pre,
store_builder,
self.backend(),
dictionary
)))
} else {
Ok(CliExecutor::Http(HttpExecutorImpl::new(
instance_pre,
store_builder,
self.backend(),
dictionary
)))
}
}
Expand Down

0 comments on commit 3696435

Please sign in to comment.