34个版本

0.8.1 2023年8月29日
0.8.0 2023年6月19日
0.7.1 2022年9月14日
0.6.1 2022年5月11日
0.1.0 2019年9月23日

#1 in #low-memory

Download history 8829/week @ 2024-04-23 6501/week @ 2024-04-30 7438/week @ 2024-05-07 8765/week @ 2024-05-14 8427/week @ 2024-05-21 8383/week @ 2024-05-28 6823/week @ 2024-06-04 8489/week @ 2024-06-11 7242/week @ 2024-06-18 8492/week @ 2024-06-25 8291/week @ 2024-07-02 10547/week @ 2024-07-09 9441/week @ 2024-07-16 7581/week @ 2024-07-23 9641/week @ 2024-07-30 7764/week @ 2024-08-06

每月下载量36,052
5 个crate中使用 (3个直接使用)

Apache-2.0

175KB
4.5K SLoC

ttrpc-rust

ttrpc-rust是containerd的一个 非核心 子项目

ttrpc-rustttrpc 的Rust版本。 ttrpc 是专为低内存环境设计的GRPC。

ttrpc-rustttrpc_rust_plugin 编译器是基于 gRPC-rs 的gRPC编译器 grpcio-compiler 进行修改的。

使用方法

1. 使用 protoc 命令生成

从proto文件生成源代码

  1. 从github.com/protocolbuffers/protobuf安装protoc

  2. 安装protobuf-codegen

cargo install --force protobuf-codegen
  1. 从ttrpc-rust/compiler安装ttrpc_rust_plugin
cd ttrpc-rust/compiler
cargo install --force --path .
  1. 生成源代码
$ protoc --rust_out=. --ttrpc_out=. --plugin=protoc-gen-ttrpc=`which ttrpc_rust_plugin` example.proto

2. 编程生成

API用于生成例如在build.rs中使用的.rs文件

示例代码

fn main() {
    protoc_rust_ttrpc::Codegen::new()
        .out_dir("protocols")
        .inputs(&[
            "protocols/protos/agent.proto",
        ])
        .include("protocols/protos")
        .rust_protobuf() // also generate protobuf messages, not just services
        .run()
        .expect("Codegen failed.");
}

async/.await

ttrpc-rust支持async/.await。通过使用async/.await,您可以减少由线程引起的开销和资源消耗。

使用方法

1. 生成异步版本的代码

目前我们只支持使用ttrpc-codegen生成异步代码

    ttrpc_codegen::Codegen::new()
        .out_dir("protocols/asynchronous")
        .inputs(&protos)
        .include("protocols/protos")
        .rust_protobuf()
        .customize(Customize {
            async_all: true, // It's the key option.
            ..Default::default()
        })
        .run()
        .expect("Gen async codes failed.");

提供自定义选项

  • async_all: 为服务器和客户端生成异步代码
  • async_server: 为服务器生成异步代码
  • async_client: 为客户端生成异步代码

更多信息请参考 example/build.rs

2. 使用async/.await的方式编写您的实现

请遵循 example/async-server.rsexample/async-client.rs 中的指南

运行示例

  1. 进入目录

    $ cd ttrpc-rust/example
    
  2. 启动服务器

    $ cargo run --example server
    

    $ cargo run --example async-server
    
  3. 启动客户端

    $ cargo run --example client
    

    $ cargo run --example async-client
    

注意:protobuf的版本

protobuf-codegen、ttrpc_rust_plugin 和您的代码应使用相同的 protobuf 版本。如果您使用不同版本的 protobuf,将会遇到以下错误。

27 | const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_8_0;
   |                                                 ^^^^^^^^^^^^^ help: a constant with a similar name exists: `VERSION_2_10_1`

原因是 由 protobuf-codegen 生成的文件仅与同一版本的运行时兼容

为了解决这个问题

  1. 使用新的 protobuf 重新构建 protobuf-codegen
cd grpc-rs
cargo clean
cargo update
cargo install --force protobuf-codegen
  1. 使用新的 protobuf 重新构建 ttrpc_rust_plugin
cd ttrpc-rust/compiler
cargo clean
cargo update
cargo install --force --path .
  1. 构建您的项目。

依赖项

~3–17MB
~193K SLoC