Coder Social home page Coder Social logo

isabella232 / ttrpc-rust Goto Github PK

View Code? Open in Web Editor NEW

This project forked from containerd/ttrpc-rust

0.0 0.0 0.0 368 KB

Rust implementation of ttrpc (GRPC for low-memory environments)

License: Apache License 2.0

Rust 98.62% Shell 1.21% Makefile 0.17%

ttrpc-rust's Introduction

ttrpc-rust

ttrpc-rust is a non-core subproject of containerd

ttrpc-rust is the Rust version of ttrpc. ttrpc is GRPC for low-memory environments.

The ttrpc compiler of ttrpc-rust ttrpc_rust_plugin is modified from gRPC compiler of gRPC-rs grpcio-compiler.

Usage

1. Generate with protoc command

To generate the sources from proto files:

  1. Install protoc from github.com/protocolbuffers/protobuf

  2. Install protobuf-codegen

cargo install --force protobuf-codegen
  1. Install ttrpc_rust_plugin from ttrpc-rust/compiler
cd ttrpc-rust/compiler
cargo install --force --path .
  1. Generate the sources:
$ protoc --rust_out=. --ttrpc_out=. --plugin=protoc-gen-ttrpc=`which ttrpc_rust_plugin` example.proto

2. Generate programmatically

API to generate .rs files to be used e. g. from build.rs.

Example code:

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 supports async/.await. By using async/.await you can reduce the overhead and resource consumption caused by threads.

Usage

1. Generate codes in async version

Currently we only support generating async codes by using 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.");

Provide customize option

  • async_all: generate async codes for both server and client
  • async_server: generate async codes for server
  • async_client: generate async codes for client

See more in example/build.rs

2. Write your implemention in async/.await's way

Please follow the guidlines in example/async-server.rs and example/async-client.rs

Run Examples

  1. Go to the directory

    $ cd ttrpc-rust/example
    
  2. Start the server

    $ cargo run --example server
    

    or

    $ cargo run --example async-server
    
  3. Start a client

    $ cargo run --example client
    

    or

    $ cargo run --example async-client
    

Notes: the version of protobuf

protobuf-codegen, ttrpc_rust_plugin and your code should use the same version protobuf. You will get following fail if use the different version protobuf.

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

The reason is that files generated by protobuf-codegen are compatible only with the same version of runtime

To fix this issue:

  1. Rebuild protobuf-codegen with new protobuf:
cd grpc-rs
cargo clean
cargo update
cargo install --force protobuf-codegen
  1. Rebuild ttrpc_rust_plugin with new protobuf:
cd ttrpc-rust/compiler
cargo clean
cargo update
cargo install --force --path .
  1. Build your project.

ttrpc-rust's People

Contributors

gnawux avatar jodh-intel avatar lifupan avatar mxpv avatar quanweizhou avatar teawater avatar tim-zhang avatar windniw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.