Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: hyperium/tonic
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.7.2
Choose a base ref
...
head repository: hyperium/tonic
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.8.0
Choose a head ref
  • 15 commits
  • 63 files changed
  • 13 contributors

Commits on May 5, 2022

  1. feat(build): Better support for custom codecs (#999)

    BREAKING CHANGE: `CODEC_PATH` moved from const to fn
    bmwill authored May 5, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    de2e4ac View commit details

Commits on May 20, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    ec9ee61 View commit details

Commits on May 23, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    91b73f9 View commit details

Commits on Jun 13, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d9f5c75 View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a585a72 View commit details

Commits on Jun 15, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8287988 View commit details

Commits on Jun 20, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    4388d82 View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    10f6d2f View commit details

Commits on Jun 21, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1d2083a View commit details
  2. feat: Decouple NamedService from the transport feature (#969)

    Co-authored-by: Eliza Weisman <eliza@buoyant.io>
    olix0r and hawkw authored Jun 21, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    feae96c View commit details

Commits on Jun 28, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    9ea03c2 View commit details

Commits on Jul 29, 2022

  1. reflection: export server types (#1043)

    Makes it easier to store a reflection server in a variable
    or get the gRPC service's name.
    zombiezen authored Jul 29, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e40ae11 View commit details
  2. reflection: add with_service_name method (#1042)

    * reflection: add with_service_name method
    
    Fixes: #682
    
    * Update tonic-reflection/src/server.rs
    
    Co-authored-by: Lucio Franco <luciofranco14@gmail.com>
    zombiezen and LucioFranco authored Jul 29, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    4288fd3 View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    2a7c610 View commit details
  4. Copy the full SHA
    ec359ba View commit details
Showing with 1,365 additions and 539 deletions.
  1. +12 −0 .github/workflows/CI.yml
  2. +17 −0 CHANGELOG.md
  3. +12 −4 examples/Cargo.toml
  4. +27 −2 examples/build.rs
  5. +4 −4 examples/helloworld-tutorial.md
  6. +3 −3 examples/routeguide-tutorial.md
  7. +4 −1 examples/src/compression/client.rs
  8. +4 −1 examples/src/compression/server.rs
  9. +28 −0 examples/src/json-codec/client.rs
  10. +80 −0 examples/src/json-codec/common.rs
  11. +48 −0 examples/src/json-codec/server.rs
  12. +3 −23 examples/src/tls/client_rustls.rs
  13. +3 −3 interop/Cargo.toml
  14. +4 −3 interop/src/bin/client.rs
  15. +2 −2 interop/src/bin/server.rs
  16. +1 −1 tests/ambiguous_methods/Cargo.toml
  17. +3 −3 tests/compression/Cargo.toml
  18. +5 −4 tests/compression/src/bidirectional_stream.rs
  19. +13 −6 tests/compression/src/client_stream.rs
  20. +10 −5 tests/compression/src/compressing_request.rs
  21. +22 −12 tests/compression/src/compressing_response.rs
  22. +9 −4 tests/compression/src/server_stream.rs
  23. +2 −2 tests/extern_path/my_application/Cargo.toml
  24. +2 −2 tests/extern_path/uuid/Cargo.toml
  25. +1 −1 tests/included_service/Cargo.toml
  26. +1 −1 tests/integration_tests/Cargo.toml
  27. +100 −0 tests/integration_tests/tests/origin.rs
  28. +1 −1 tests/root-crate-path/Cargo.toml
  29. +1 −1 tests/same_name/Cargo.toml
  30. +1 −1 tests/service_named_service/Cargo.toml
  31. +1 −1 tests/stream_conflict/Cargo.toml
  32. +2 −2 tests/wellknown-compiled/Cargo.toml
  33. +2 −2 tests/wellknown/Cargo.toml
  34. +2 −3 tonic-build/Cargo.toml
  35. +16 −10 tonic-build/src/client.rs
  36. +5 −6 tonic-build/src/lib.rs
  37. +482 −0 tonic-build/src/manual.rs
  38. +42 −6 tonic-build/src/prost.rs
  39. +22 −44 tonic-build/src/server.rs
  40. +5 −5 tonic-health/Cargo.toml
  41. +6 −6 tonic-reflection/Cargo.toml
  42. +17 −3 tonic-reflection/src/server.rs
  43. +4 −4 tonic-types/Cargo.toml
  44. +1 −1 tonic-types/src/lib.rs
  45. +3 −3 tonic-web/Cargo.toml
  46. +1 −1 tonic-web/src/lib.rs
  47. +1 −1 tonic-web/tests/integration/Cargo.toml
  48. +5 −5 tonic/Cargo.toml
  49. +52 −71 tonic/src/client/grpc.rs
  50. +55 −18 tonic/src/codec/compression.rs
  51. +50 −82 tonic/src/codec/decode.rs
  52. +27 −44 tonic/src/codec/encode.rs
  53. +0 −3 tonic/src/codec/mod.rs
  54. +0 −1 tonic/src/codegen.rs
  55. +1 −1 tonic/src/lib.rs
  56. +2 −2 tonic/src/response.rs
  57. +23 −96 tonic/src/server/grpc.rs
  58. +9 −0 tonic/src/server/mod.rs
  59. +22 −1 tonic/src/transport/channel/endpoint.rs
  60. +52 −2 tonic/src/transport/server/incoming.rs
  61. +2 −10 tonic/src/transport/server/mod.rs
  62. +25 −14 tonic/src/transport/service/add_origin.rs
  63. +5 −1 tonic/src/transport/service/connection.rs
12 changes: 12 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -26,6 +26,10 @@ jobs:
run: rustup component add rustfmt
- name: Install cargo-hack
run: cargo install cargo-hack
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- uses: Swatinem/rust-cache@v1
- name: Check fmt
run: cargo fmt -- --check
@@ -59,6 +63,10 @@ jobs:
rust-version: ${{ matrix.rust }}
- name: Install rustfmt
run: rustup component add rustfmt
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- uses: Swatinem/rust-cache@v1
- uses: actions/checkout@master
- name: Run tests
@@ -82,6 +90,10 @@ jobs:
- name: Install rustfmt
run: rustup component add rustfmt
- uses: actions/checkout@master
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- uses: Swatinem/rust-cache@v1
- name: Run interop tests
run: ./interop/test.sh
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# [v0.8.0](https://github.com/hyperium/tonic/compare/v0.7.2...v0.8.0) (2022-07-29)


### Features

* Add `Grpc::with_origin` for clients ([#1017](https://github.com/hyperium/tonic/issues/1017)) ([10f6d2f](https://github.com/hyperium/tonic/commit/10f6d2f1a9fa3969599ebd674f7be27f4f458754))
* **build:** Add option to emit rerun-if-changed instructions ([#1021](https://github.com/hyperium/tonic/issues/1021)) ([1d2083a](https://github.com/hyperium/tonic/commit/1d2083a1a690edcb3f95343edfe229339c4257b7))
* **build:** Better support for custom codecs ([#999](https://github.com/hyperium/tonic/issues/999)) ([de2e4ac](https://github.com/hyperium/tonic/commit/de2e4ac077c076736dc451f3415ea7da1a61a560))
* Decouple `NamedService` from the `transport` feature ([#969](https://github.com/hyperium/tonic/issues/969)) ([feae96c](https://github.com/hyperium/tonic/commit/feae96c5be1247af368e6ce665c8df757d298e35))
* reflection: Export server types.
* reflection: Add `with_service_name`.

### BREAKING CHANGES

* **build:** `CODEC_PATH` moved from const to fn
* **tonic** Remove codegen depedency on `compression` feature.

# [v0.7.2](https://github.com/hyperium/tonic/compare/v0.7.1...v0.7.2) (2022-05-04)


16 changes: 12 additions & 4 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -186,13 +186,21 @@ path = "src/streaming/client.rs"
name = "streaming-server"
path = "src/streaming/server.rs"

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

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

[dependencies]
async-stream = "0.3"
futures = { version = "0.3", default-features = false, features = ["alloc"] }
prost = "0.10"
prost = "0.11"
tokio = { version = "1.0", features = [ "rt-multi-thread", "time", "fs", "macros", "net",] }
tokio-stream = { version = "0.1", features = ["net"] }
tonic = { path = "../tonic", features = ["tls", "compression"] }
tonic = { path = "../tonic", features = ["tls", "gzip"] }
tower = { version = "0.4" }
# Required for routeguide
rand = "0.8"
@@ -204,7 +212,7 @@ tracing-attributes = "0.1"
tracing-futures = "0.2"
tracing-subscriber = { version = "0.3", features = ["tracing-log"] }
# Required for wellknown types
prost-types = "0.10"
prost-types = "0.11"
# Hyper example
http = "0.2"
http-body = "0.4.2"
@@ -229,4 +237,4 @@ tower-http = { version = "0.3", features = ["add-extension", "util"] }


[build-dependencies]
tonic-build = { path = "../tonic-build", features = ["prost", "compression"] }
tonic-build = { path = "../tonic-build", features = ["prost"] }
29 changes: 27 additions & 2 deletions examples/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env;
use std::path::PathBuf;
use std::{env, path::PathBuf};

fn main() {
tonic_build::configure()
@@ -30,4 +29,30 @@ fn main() {
&["proto/googleapis"],
)
.unwrap();

build_json_codec_service();
}

// Manually define the json.helloworld.Greeter service which used a custom JsonCodec to use json
// serialization instead of protobuf for sending messages on the wire.
// This will result in generated client and server code which relies on its request, response and
// codec types being defined in a module `crate::common`.
//
// See the client/server examples defined in `src/json-codec` for more information.
fn build_json_codec_service() {
let greeter_service = tonic_build::manual::Service::builder()
.name("Greeter")
.package("json.helloworld")
.method(
tonic_build::manual::Method::builder()
.name("say_hello")
.route_name("SayHello")
.input_type("crate::common::HelloRequest")
.output_type("crate::common::HelloResponse")
.codec_path("crate::common::JsonCodec")
.build(),
)
.build();

tonic_build::manual::Builder::new().compile(&[greeter_service]);
}
8 changes: 4 additions & 4 deletions examples/helloworld-tutorial.md
Original file line number Diff line number Diff line change
@@ -112,19 +112,19 @@ name = "helloworld-client"
path = "src/client.rs"

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

[build-dependencies]
tonic-build = "0.7"
tonic-build = "0.8"
```

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:

## Generating Server and Client code

At the root of your project, create a `build.rs` file and add the following code:
At the root of your project (not /src), create a `build.rs` file and add the following code:

```rust
fn main() -> Result<(), Box<dyn std::error::Error>> {
6 changes: 3 additions & 3 deletions examples/routeguide-tutorial.md
Original file line number Diff line number Diff line change
@@ -174,8 +174,8 @@ Edit `Cargo.toml` and add all the dependencies we'll need for this example:

```toml
[dependencies]
tonic = "0.7"
prost = "0.10"
tonic = "0.8"
prost = "0.11"
futures-core = "0.3"
futures-util = "0.3"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros", "sync", "time"] }
@@ -187,7 +187,7 @@ serde_json = "1.0"
rand = "0.7"

[build-dependencies]
tonic-build = "0.7"
tonic-build = "0.8"
```

Create a `build.rs` file at the root of your crate:
5 changes: 4 additions & 1 deletion examples/src/compression/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use tonic::codec::CompressionEncoding;
use tonic::transport::Channel;

pub mod hello_world {
@@ -13,7 +14,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await
.unwrap();

let mut client = GreeterClient::new(channel).send_gzip().accept_gzip();
let mut client = GreeterClient::new(channel)
.send_compressed(CompressionEncoding::Gzip)
.accept_compressed(CompressionEncoding::Gzip);

let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
5 changes: 4 additions & 1 deletion examples/src/compression/server.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
use tonic::codec::CompressionEncoding;

pub mod hello_world {
tonic::include_proto!("helloworld");
@@ -32,7 +33,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("GreeterServer listening on {}", addr);

let service = GreeterServer::new(greeter).send_gzip().accept_gzip();
let service = GreeterServer::new(greeter)
.send_compressed(CompressionEncoding::Gzip)
.accept_compressed(CompressionEncoding::Gzip);

Server::builder().add_service(service).serve(addr).await?;

28 changes: 28 additions & 0 deletions examples/src/json-codec/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! A HelloWorld example that uses JSON instead of protobuf as the message serialization format.
//!
//! Generated code is the output of codegen as defined in the `build_json_codec_service` function
//! in the `examples/build.rs` file. As defined there, the generated code assumes that a module
//! `crate::common` exists which defines `HelloRequest`, `HelloResponse`, and `JsonCodec`.
pub mod common;
use common::HelloRequest;

pub mod hello_world {
include!(concat!(env!("OUT_DIR"), "/json.helloworld.Greeter.rs"));
}
use hello_world::greeter_client::GreeterClient;

#[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(())
}
80 changes: 80 additions & 0 deletions examples/src/json-codec/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! This module defines common request/response types as well as the JsonCodec that is used by the
//! json.helloworld.Greeter service which is defined manually (instead of via proto files) by the
//! `build_json_codec_service` function in the `examples/build.rs` file.
use bytes::{Buf, BufMut};
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use tonic::{
codec::{Codec, DecodeBuf, Decoder, EncodeBuf, Encoder},
Status,
};

#[derive(Debug, Deserialize, Serialize)]
pub struct HelloRequest {
pub name: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct HelloResponse {
pub message: String,
}

#[derive(Debug)]
pub struct JsonEncoder<T>(PhantomData<T>);

impl<T: serde::Serialize> Encoder for JsonEncoder<T> {
type Item = T;
type Error = Status;

fn encode(&mut self, item: Self::Item, buf: &mut EncodeBuf<'_>) -> Result<(), Self::Error> {
serde_json::to_writer(buf.writer(), &item).map_err(|e| Status::internal(e.to_string()))
}
}

#[derive(Debug)]
pub struct JsonDecoder<U>(PhantomData<U>);

impl<U: serde::de::DeserializeOwned> Decoder for JsonDecoder<U> {
type Item = U;
type Error = Status;

fn decode(&mut self, buf: &mut DecodeBuf<'_>) -> Result<Option<Self::Item>, Self::Error> {
if !buf.has_remaining() {
return Ok(None);
}

let item: Self::Item =
serde_json::from_reader(buf.reader()).map_err(|e| Status::internal(e.to_string()))?;
Ok(Some(item))
}
}

/// A [`Codec`] that implements `application/grpc+json` via the serde library.
#[derive(Debug, Clone)]
pub struct JsonCodec<T, U>(PhantomData<(T, U)>);

impl<T, U> Default for JsonCodec<T, U> {
fn default() -> Self {
Self(PhantomData)
}
}

impl<T, U> Codec for JsonCodec<T, U>
where
T: serde::Serialize + Send + 'static,
U: serde::de::DeserializeOwned + Send + 'static,
{
type Encode = T;
type Decode = U;
type Encoder = JsonEncoder<T>;
type Decoder = JsonDecoder<U>;

fn encoder(&mut self) -> Self::Encoder {
JsonEncoder(PhantomData)
}

fn decoder(&mut self) -> Self::Decoder {
JsonDecoder(PhantomData)
}
}
48 changes: 48 additions & 0 deletions examples/src/json-codec/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! A HelloWorld example that uses JSON instead of protobuf as the message serialization format.
//!
//! Generated code is the output of codegen as defined in the `build_json_codec_service` function
//! in the `examples/build.rs` file. As defined there, the generated code assumes that a module
//! `crate::common` exists which defines `HelloRequest`, `HelloResponse`, and `JsonCodec`.
use tonic::{transport::Server, Request, Response, Status};

pub mod common;
use common::{HelloRequest, HelloResponse};

pub mod hello_world {
include!(concat!(env!("OUT_DIR"), "/json.helloworld.Greeter.rs"));
}
use hello_world::greeter_server::{Greeter, GreeterServer};

#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
request: Request<HelloRequest>,
) -> Result<Response<HelloResponse>, Status> {
println!("Got a request from {:?}", request.remote_addr());

let reply = HelloResponse {
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(())
}
Loading