2.7 KiB
2.7 KiB
Tasks for Tonic Integration
This document outlines the steps required to integrate the roto protobuf library with the tonic gRPC framework.
Goals
- Provide a
tonic::codec::Codecimplementation forrotomessages. - Enable zero-allocation decoding of gRPC requests and responses.
- Support efficient encoding of gRPC messages using
roto'sBuilderpattern. - Generate
tonic-compatible service traits and client/server boilerplate viaprotoc-gen-roto.
1. Runtime Changes (roto_runtime)
- Add
bytesas a dependency toroto_runtime. - Modify
ProtoBuilderto support writing tobytes::BufMutor provide a specializedBufMutbased builder to facilitate integration withtonic::codec::EncodeBuf. - Design and implement "Owned" message support:
- Create a mechanism (likely via codegen) to generate structs that hold
bytes::Bytesand the field offsets (e.g.,OwnedMyMessage). - These structs will serve as the owned types required by
tonic'sCodec. - Add a method to convert an
OwnedMyMessageinto a zero-allocationReader(e.g.,reader(&self) -> MyMessage<'_>).
- Create a mechanism (likely via codegen) to generate structs that hold
2. Tonic Codec Implementation
- Implement
tonic::codec::Codecforrotomessages. - Implement
tonic::codec::Decoderforrotomessages:- The decoder should take a
DecodeBufand produce anOwnedMyMessage. - It must perform the initial scan of the buffer to populate the field offsets.
- The decoder should take a
- Implement
tonic::codec::Encoderforrotomessages:- The encoder should take an
OwnedMyMessageand write its internalbytes::Bytesto theEncodeBuf. - Since
rotoresponses are built usingBuilderdirectly into a buffer, theEncoderwill primarily handle copying these pre-built buffers.
- The encoder should take an
3. Code Generation Extensions (protoc-gen-roto)
- Update the generator to produce
OwnedMyMessagestructs in addition to theReaderandBuilderstructs. - Generate the
OwnedMyMessage::new(data: bytes::Bytes)method for initial decoding. - Implement gRPC service generation:
- Generate
tonic-compatible service traits (using#[tonic::async_trait]). - Generate client and server boilerplate that uses
OwnedMyMessageas the request and response types. - Ensure the generated code correctly maps protobuf services to Rust traits.
- Generate
4. Testing and Validation
- Create a test gRPC project using the updated
protoc-gen-roto. - Implement a sample gRPC service with:
- A unary call.
- A server-streaming call.
- A client-streaming call.
- A bidirectional-streaming call.
- Verify that the integration is zero-allocation on the reading path.
- Perform benchmark tests to compare performance with
prost.