Skip to content

Commit

Permalink
Merge branch 'hyperium:master' into grpc-method
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto authored Mar 1, 2024
2 parents aff7544 + 8a53392 commit ee0a480
Show file tree
Hide file tree
Showing 43 changed files with 709 additions and 346 deletions.
20 changes: 11 additions & 9 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
rustfmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
with:
components: rustfmt
Expand All @@ -25,13 +25,13 @@ jobs:
name: cargo-deny check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: EmbarkStudios/cargo-deny-action@v1

codegen:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
- name: Install protoc
uses: taiki-e/install-action@v2
Expand All @@ -51,8 +51,10 @@ jobs:
- os: windows-latest
option: --exclude-features uds
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@nightly
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2024-02-06
- uses: taiki-e/install-action@cargo-hack
- uses: taiki-e/install-action@cargo-udeps
- name: Install protoc
Expand All @@ -68,7 +70,7 @@ jobs:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
- uses: taiki-e/install-action@cargo-hack
- name: Install protoc
Expand All @@ -85,7 +87,7 @@ jobs:
name: Check MSRV
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
with:
rust-version: "1.70" # msrv
Expand All @@ -105,7 +107,7 @@ jobs:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
- name: Install protoc
uses: taiki-e/install-action@v2
Expand All @@ -123,7 +125,7 @@ jobs:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
- name: Install protoc
uses: taiki-e/install-action@v2
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# [0.11.0](https://github.com/hyperium/tonic/compare/v0.10.2...v0.11.0) (2024-02-08)

BREAKING CHANGES:

- Removed `NamedService` from the `transport` module, please import it via
`tonic::server::NamedService`.
- MSRV bumped to `1.70`.

### Features

- Added `zstd` compression support.
- Added connection timeout for `connecto_with_connector_lazy`.
- Upgrade rustls to `v0.22`
- Feature gate server implementation for `tonic-reflection`.


# [0.10.2](https://github.com/hyperium/tonic/compare/v0.10.1...v0.10.2) (2023-09-28)


Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ members = [
"tests/default_stubs",
]
resolver = "2"

1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ skip-tree = [
{ name = "syn" },
{ name = "bitflags" },
{ name = "indexmap" },
{ name = "examples" },
]

[licenses]
Expand Down
8 changes: 8 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ required-features = ["cancellation"]
name = "cancellation-client"
path = "src/cancellation/client.rs"

[[bin]]
name = "codec-buffers-server"
path = "src/codec_buffers/server.rs"

[[bin]]
name = "codec-buffers-client"
path = "src/codec_buffers/client.rs"


[features]
gcp = ["dep:prost-types", "tonic/tls"]
Expand Down
8 changes: 8 additions & 0 deletions examples/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ fn main() {
.unwrap();

build_json_codec_service();

let smallbuff_copy = out_dir.join("smallbuf");
let _ = std::fs::create_dir(smallbuff_copy.clone()); // This will panic below if the directory failed to create
tonic_build::configure()
.out_dir(smallbuff_copy)
.codec_path("crate::common::SmallBufferCodec")
.compile(&["proto/helloworld/helloworld.proto"], &["proto"])
.unwrap();
}

// Manually define the json.helloworld.Greeter service which used a custom JsonCodec to use json
Expand Down
8 changes: 4 additions & 4 deletions examples/helloworld-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ name = "helloworld-client"
path = "src/client.rs"

[dependencies]
tonic = "0.10"
tonic = "0.11"
prost = "0.12"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }

[build-dependencies]
tonic-build = "0.10"
tonic-build = "0.11"
```

We include `tonic-build` as a useful way to incorporate the generation of our client and server gRPC code into the build process of our application. We will setup this build process now:
Expand Down Expand Up @@ -167,7 +167,7 @@ impl Greeter for MyGreeter {
println!("Got a request: {:?}", request);

let reply = hello_world::HelloReply {
message: format!("Hello {}!", request.into_inner().name).into(), // We must use .into_inner() as the fields of gRPC requests and responses are private
message: format!("Hello {}!", request.into_inner().name), // We must use .into_inner() as the fields of gRPC requests and responses are private
};

Ok(Response::new(reply)) // Send back our formatted greeting
Expand Down Expand Up @@ -216,7 +216,7 @@ impl Greeter for MyGreeter {
println!("Got a request: {:?}", request);

let reply = hello_world::HelloReply {
message: format!("Hello {}!", request.into_inner().name).into(),
message: format!("Hello {}!", request.into_inner().name),
};

Ok(Response::new(reply))
Expand Down
4 changes: 2 additions & 2 deletions examples/routeguide-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Edit `Cargo.toml` and add all the dependencies we'll need for this example:

```toml
[dependencies]
tonic = "0.10"
tonic = "0.11"
prost = "0.12"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros", "sync", "time"] }
tokio-stream = "0.1"
Expand All @@ -185,7 +185,7 @@ serde_json = "1.0"
rand = "0.7"

[build-dependencies]
tonic-build = "0.10"
tonic-build = "0.11"
```

Create a `build.rs` file at the root of your crate:
Expand Down
30 changes: 30 additions & 0 deletions examples/src/codec_buffers/client.rs
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(())
}
41 changes: 41 additions & 0 deletions examples/src/codec_buffers/common.rs
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))
}
}
51 changes: 51 additions & 0 deletions examples/src/codec_buffers/server.rs
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(())
}
2 changes: 0 additions & 2 deletions examples/src/tls_rustls/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ pub mod pb {
tonic::include_proto!("/grpc.examples.unaryecho");
}

use std::iter::FromIterator;

use hyper::{client::HttpConnector, Uri};
use pb::{echo_client::EchoClient, EchoRequest};
use tokio_rustls::rustls::{ClientConfig, RootCertStore};
Expand Down
7 changes: 2 additions & 5 deletions interop/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,12 @@ where
}

fn call(&mut self, req: http::Request<hyper::Body>) -> Self::Future {
let echo_header = req
.headers()
.get("x-grpc-test-echo-initial")
.map(Clone::clone);
let echo_header = req.headers().get("x-grpc-test-echo-initial").cloned();

let echo_trailer = req
.headers()
.get("x-grpc-test-echo-trailing-bin")
.map(Clone::clone)
.cloned()
.map(|v| (HeaderName::from_static("x-grpc-test-echo-trailing-bin"), v));

let call = self.inner.call(req);
Expand Down
4 changes: 2 additions & 2 deletions tonic-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ categories = ["network-programming", "asynchronous"]
description = """
Codegen module of `tonic` gRPC implementation.
"""
documentation = "https://docs.rs/tonic-build/0.10.2"
documentation = "https://docs.rs/tonic-build/0.11.0"
edition = "2021"
homepage = "https://github.com/hyperium/tonic"
keywords = ["rpc", "grpc", "async", "codegen", "protobuf"]
license = "MIT"
name = "tonic-build"
readme = "README.md"
repository = "https://github.com/hyperium/tonic"
version = "0.10.2"
version = "0.11.0"

[dependencies]
prettyplease = { version = "0.2" }
Expand Down
2 changes: 1 addition & 1 deletion tonic-build/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# tonic-build

Compiles proto files via prost and generates service stubs and proto definitiones for use with tonic.
Compiles proto files via prost and generates service stubs and proto definitions for use with tonic.

## Features

Expand Down
14 changes: 14 additions & 0 deletions tonic-build/src/compile_settings.rs
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(),
}
}
}
4 changes: 3 additions & 1 deletion tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
html_logo_url = "https://raw.githubusercontent.com/tokio-rs/website/master/public/img/icons/tonic.svg"
)]
#![deny(rustdoc::broken_intra_doc_links)]
#![doc(html_root_url = "https://docs.rs/tonic-build/0.10.2")]
#![doc(html_root_url = "https://docs.rs/tonic-build/0.11.0")]
#![doc(issue_tracker_base_url = "https://github.com/hyperium/tonic/issues/")]
#![doc(test(no_crate_inject, attr(deny(rust_2018_idioms))))]
#![cfg_attr(docsrs, feature(doc_cfg))]
Expand All @@ -97,6 +97,8 @@ pub mod server;
mod code_gen;
pub use code_gen::CodeGenBuilder;

mod compile_settings;

/// Service generation trait.
///
/// This trait can be implemented and consumed
Expand Down
Loading

0 comments on commit ee0a480

Please sign in to comment.