generate wrpc-server based on wit-bindings and homogenize the wrpc-client and wasmtime-bindings

未关闭
#1,424 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
冷清
技术栈
rust, wasm

调研方向

从示例的 client_bindings、server_bindings 和 host_bindings 生成块以及 ../wit 接口定义开始。将生成的 wrpc 客户端和服务器 API 与 wasmtime bindings 进行比较,然后确定能够从 WIT bindings 创建服务器的最小宏或生成变更。完成的标准是减少 boilerplate,并提供一致的客户端/服务器 binding 接口,同时示例仍支持 local、client 和 server 模式。

由索引模型根据 Issue 内容生成。

描述

Did a quick experiment to build an isomorphic wasm component that could be used locally or run remotely on a cluster. As I understand wrpc uses the same interface description but has a completely different binary serialization, and because of that the API's between wit-bindgen and wrpc-bindgen are completely different. However if the below example could be made less boilerplatte heavy (by creating a macro that implements a wrpc server based on the wit bindings) it would be cool. I can also imagine extending it to frontend components which export their html as a string. This way the component could be run server side for indexing or client side with a webview or wrapped into a browser module with jco.

use anyhow::Result;
use clap::Parser;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use wasi_hw_native::WasmCtx;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Mode {
    Local,
    Client,
    Server,
}

impl std::str::FromStr for Mode {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        Ok(match s {
            "local" => Mode::Local,
            "client" => Mode::Client,
            "server" => Mode::Server,
            _ => anyhow::bail!("invalid mode {s}"),
        })
    }
}

#[derive(Parser)]
struct Opts {
    #[arg(short, long, default_value = "local")]
    mode: Mode,
    #[arg(
        short,
        long,
        default_value = "../target/wasm32-wasip2/release/wasi_hw.wasm"
    )]
    file: PathBuf,
    #[arg(short, long, default_value = "127.0.0.1:8080")]
    address: String,
}

mod client_bindings {
    wit_bindgen_wrpc::generate!({
        world: "hello-world-consumer",
        path: "../wit",
    });
}

mod server_bindings {
    wit_bindgen_wrpc::generate!({
        world: "hello-world-provider",
        path: "../wit",
    });
}

mod host_bindings {
    wasmtime::component::bindgen!({
        world: "hello-world-provider",
        path: "../wit",
    });
}

pub struct CustomWasmCtx {
    ctx: WasmCtx,
}

impl CustomWasmCtx {
    pub fn new(path: &Path) -> Result<Self> {
        Ok(Self {
            ctx: WasmCtx::new(path)?,
        })
    }

    pub async fn local_hello_hello_world_call_hello_world(&self) -> Result<String> {
        let (mut store, instance) = self.ctx.instantiate().await?;
        let bindings = host_bindings::HelloWorldProvider::new(&mut store, &instance)?;
        let result = store
            .run_concurrent(async |accessor| {
                bindings
                    .local_hello_hello_world()
                    .call_hello_world(accessor)
                    .await
            })
            .await??;
        Ok(result)
    }
}

#[derive(Clone)]
struct Server {
    ctx: Arc<CustomWasmCtx>,
}

impl Server {
    pub fn new(ctx: CustomWasmCtx) -> Self {
        Self { ctx: Arc::new(ctx) }
    }
}

impl server_bindings::exports::local::hello::hello_world::Handler<SocketAddr> for Server {
    async fn hello_world(&self, _ctx: SocketAddr) -> anyhow::Result<String> {
        self.ctx.local_hello_hello_world_call_hello_world().await
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt::init();
    let opts = Opts::parse();
    if opts.mode == Mode::Client {
        let transport = wrpc_transport::tcp::Client::from(opts.address);
        let result =
            client_bindings::local::hello::hello_world::hello_world(&transport, ()).await?;
        println!("result: {}", result);
        return Ok(());
    }

    let ctx = CustomWasmCtx::new(&opts.file)?;

    if opts.mode == Mode::Local {
        let result = ctx.local_hello_hello_world_call_hello_world().await?;
        println!("result: {}", result);
        return Ok(());
    }

    let server = Server::new(ctx);
    let transport = Arc::new(wrpc_transport::Server::default());
    let accept = wasi_hw_native::tcp_server(&opts.address, transport.clone()).await?;
    let invocation = server_bindings::serve(transport.as_ref(), server).await?;
    wasi_hw_native::wrpc_server(accept, invocation).await?;
    Ok(())
}
主要语言
Rust
星标
336
派生
48
平均合并
10 小时 39 分钟
30 天内合并 PR
203

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bytecodealliance/wrpc 的其他 Issue

查看 bytecodealliance/wrpc 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。