-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hyperium:master' into grpc-method
- Loading branch information
Showing
43 changed files
with
709 additions
and
346 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 |
---|---|---|
|
@@ -28,4 +28,3 @@ members = [ | |
"tests/default_stubs", | ||
] | ||
resolver = "2" | ||
|
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
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,41 @@ | ||
//! 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, ProstCodec}; | ||
|
||
#[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 = <ProstCodec<T, U> as Codec>::Encoder; | ||
type Decoder = <ProstCodec<T, U> as Codec>::Decoder; | ||
|
||
fn encoder(&mut self) -> Self::Encoder { | ||
// Here, we will just customize the prost codec's internal buffer settings. | ||
// You can of course implement a complete Codec, Encoder, and Decoder if | ||
// you wish! | ||
ProstCodec::<T, U>::raw_encoder(BufferSettings::new(512, 4096)) | ||
} | ||
|
||
fn decoder(&mut self) -> Self::Decoder { | ||
ProstCodec::<T, U>::raw_decoder(BufferSettings::new(512, 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
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,14 @@ | ||
#[derive(Debug, Clone)] | ||
pub(crate) struct CompileSettings { | ||
#[cfg(feature = "prost")] | ||
pub(crate) codec_path: String, | ||
} | ||
|
||
impl Default for CompileSettings { | ||
fn default() -> Self { | ||
Self { | ||
#[cfg(feature = "prost")] | ||
codec_path: "tonic::codec::ProstCodec".to_string(), | ||
} | ||
} | ||
} |
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.