forked from hyperium/tonic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tonic): Custom codecs for generated code
Broadly, this change does 2 things: 1. Allow the built-in Prost codec to have its buffer sizes customized 2. Allow users to specify custom codecs on the tonic_build::prost::Builder The Prost codec is convenient, and handles any normal use case. However, the buffer sizes today are too large in some cases - and they may grow too aggressively. By exposing BufferSettings, users can make a small custom codec with their own BufferSettings to control their memory usage - or give enormous buffers to rpc's, as their use case requires. While one can define a custom service and methods with a custom codec today explicitly in Rust, the code generator does not have a means to supply a custom codec. I've reached for .codec... on the tonic_build::prost::Builder many times and keep forgetting it's not there. This change adds .codec_path to the Builder, so people can simply add their custom buffer codec or even their own full top level codec without reaching for manual service definition. This change is cherry picked from hyperium#1599 and applied on top of v0.10.2 release to investigate a p999 latency increase.
- Loading branch information
Showing
18 changed files
with
426 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//! A HelloWorld example that uses a custom codec instead of the default Prost codec. | ||
//! | ||
//! Generated code is the output of codegen as defined in the `examples/build.rs` file. | ||
//! The generation is the one with .codec_path("crate::common::SmallBufferCodec") | ||
//! The generated code assumes that a module `crate::common` exists which defines | ||
//! `SmallBufferCodec`, and `SmallBufferCodec` must have a Default implementation. | ||
pub mod common; | ||
|
||
pub mod small_buf { | ||
include!(concat!(env!("OUT_DIR"), "/smallbuf/helloworld.rs")); | ||
} | ||
use small_buf::greeter_client::GreeterClient; | ||
|
||
use crate::small_buf::HelloRequest; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut client = GreeterClient::connect("http://[::1]:50051").await?; | ||
|
||
let request = tonic::Request::new(HelloRequest { | ||
name: "Tonic".into(), | ||
}); | ||
|
||
let response = client.say_hello(request).await?; | ||
|
||
println!("RESPONSE={:?}", response); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! This module defines a common encoder with small buffers. This is useful | ||
//! when you have many concurrent RPC's, and not a huge volume of data per | ||
//! rpc normally. | ||
//! | ||
//! Note that you can customize your codecs per call to the code generator's | ||
//! compile function. This lets you group services by their codec needs. | ||
//! | ||
//! While this codec demonstrates customizing the built-in Prost codec, you | ||
//! can use this to implement other codecs as well, as long as they have a | ||
//! Default implementation. | ||
use std::marker::PhantomData; | ||
|
||
use prost::Message; | ||
use tonic::codec::{BufferSettings, Codec, ProstDecoder, ProstEncoder}; | ||
|
||
#[derive(Debug, Clone, Copy, Default)] | ||
pub struct SmallBufferCodec<T, U>(PhantomData<(T, U)>); | ||
|
||
impl<T, U> Codec for SmallBufferCodec<T, U> | ||
where | ||
T: Message + Send + 'static, | ||
U: Message + Default + Send + 'static, | ||
{ | ||
type Encode = T; | ||
type Decode = U; | ||
|
||
type Encoder = ProstEncoder<T>; | ||
type Decoder = ProstDecoder<U>; | ||
|
||
fn encoder(&mut self) -> Self::Encoder { | ||
ProstEncoder::new(BufferSettings { | ||
buffer_size: 512, | ||
yield_threshold: 4096, | ||
}) | ||
} | ||
|
||
fn decoder(&mut self) -> Self::Decoder { | ||
ProstDecoder::new(BufferSettings { | ||
buffer_size: 512, | ||
yield_threshold: 4096, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//! A HelloWorld example that uses a custom codec instead of the default Prost codec. | ||
//! | ||
//! Generated code is the output of codegen as defined in the `examples/build.rs` file. | ||
//! The generation is the one with .codec_path("crate::common::SmallBufferCodec") | ||
//! The generated code assumes that a module `crate::common` exists which defines | ||
//! `SmallBufferCodec`, and `SmallBufferCodec` must have a Default implementation. | ||
use tonic::{transport::Server, Request, Response, Status}; | ||
|
||
pub mod common; | ||
|
||
pub mod small_buf { | ||
include!(concat!(env!("OUT_DIR"), "/smallbuf/helloworld.rs")); | ||
} | ||
use small_buf::{ | ||
greeter_server::{Greeter, GreeterServer}, | ||
HelloReply, HelloRequest, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct MyGreeter {} | ||
|
||
#[tonic::async_trait] | ||
impl Greeter for MyGreeter { | ||
async fn say_hello( | ||
&self, | ||
request: Request<HelloRequest>, | ||
) -> Result<Response<HelloReply>, Status> { | ||
println!("Got a request from {:?}", request.remote_addr()); | ||
|
||
let reply = HelloReply { | ||
message: format!("Hello {}!", request.into_inner().name), | ||
}; | ||
Ok(Response::new(reply)) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let addr = "[::1]:50051".parse().unwrap(); | ||
let greeter = MyGreeter::default(); | ||
|
||
println!("GreeterServer listening on {}", addr); | ||
|
||
Server::builder() | ||
.add_service(GreeterServer::new(greeter)) | ||
.serve(addr) | ||
.await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::{ | ||
marker::PhantomData, | ||
mem::take, | ||
sync::{Mutex, MutexGuard}, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub(crate) struct CompileSettings { | ||
pub(crate) codec_path: String, | ||
} | ||
|
||
impl Default for CompileSettings { | ||
fn default() -> Self { | ||
Self { | ||
codec_path: "tonic::codec::ProstCodec".to_string(), | ||
} | ||
} | ||
} | ||
|
||
thread_local! { | ||
static COMPILE_SETTINGS: Mutex<Option<CompileSettings>> = Default::default(); | ||
} | ||
|
||
/// Called before compile, this installs a CompileSettings in the current thread's | ||
/// context, so that live code generation can access the settings. | ||
/// The previous state is restored when you drop the SettingsGuard. | ||
pub(crate) fn set_context(new_settings: CompileSettings) -> SettingsGuard { | ||
COMPILE_SETTINGS.with(|settings| { | ||
let mut guard = settings | ||
.lock() | ||
.expect("threadlocal mutex should always succeed"); | ||
let old_settings = guard.clone(); | ||
*guard = Some(new_settings); | ||
SettingsGuard { | ||
previous_settings: old_settings, | ||
_pd: PhantomData, | ||
} | ||
}) | ||
} | ||
|
||
/// Access the current compile settings. This is populated only during | ||
/// code generation compile() or compile_with_config() time. | ||
pub(crate) fn load() -> CompileSettings { | ||
COMPILE_SETTINGS.with(|settings| { | ||
settings | ||
.lock() | ||
.expect("threadlocal mutex should always succeed") | ||
.clone() | ||
.unwrap_or_default() | ||
}) | ||
} | ||
|
||
type PhantomUnsend = PhantomData<MutexGuard<'static, ()>>; | ||
|
||
pub(crate) struct SettingsGuard { | ||
previous_settings: Option<CompileSettings>, | ||
_pd: PhantomUnsend, | ||
} | ||
|
||
impl Drop for SettingsGuard { | ||
fn drop(&mut self) { | ||
COMPILE_SETTINGS.with(|settings| { | ||
let mut guard = settings | ||
.lock() | ||
.expect("threadlocal mutex should always succeed"); | ||
*guard = take(&mut self.previous_settings); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.