Skip to content

Commit 58478ed

Browse files
committed
Merge remote-tracking branch 'source/master' into grpc-tonic-transport-1
2 parents 4ac449f + 5687ccb commit 58478ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2788
-661
lines changed

.github/workflows/CI.yml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,74 @@ jobs:
2525
components: rustfmt
2626
- run: cargo fmt --all --check
2727

28+
build-protoc-plugin:
29+
runs-on: ${{ matrix.os }}
30+
strategy:
31+
matrix:
32+
os: [ubuntu-latest, macOS-latest, windows-latest]
33+
outputs:
34+
cache-hit: ${{ steps.cache-plugin.outputs.cache-hit }}
35+
steps:
36+
- uses: actions/checkout@v4
37+
- name: Cache protoc plugin
38+
id: cache-plugin
39+
uses: actions/cache@v4
40+
with:
41+
path: ${{ runner.temp }}/protoc-plugin
42+
# The key changes only when plugin source files change
43+
key: ${{ runner.os }}-protoc-plugin-${{ hashFiles('protoc-gen-rust-grpc/src/**', 'protoc-gen-rust-grpc/.bazelrc', 'protoc-gen-rust-grpc/MODULE.bazel') }}
44+
- name: Install Bazel
45+
if: steps.cache-plugin.outputs.cache-hit != 'true'
46+
uses: bazel-contrib/setup-bazel@0.15.0
47+
with:
48+
# Avoid downloading Bazel every time.
49+
bazelisk-cache: true
50+
# Store build cache per workflow.
51+
disk-cache: ${{ github.workflow }}
52+
# Share repository cache between workflows.
53+
repository-cache: true
54+
module-root: ./protoc-gen-rust-grpc
55+
# Building the protoc plugin from scratch takes 6–14 minutes, depending on
56+
# the OS. This delays the execution of workflows that use the plugin in
57+
# build.rs files. We try to avoid rebuilding the plugin if it hasn't
58+
# changed.
59+
- name: Build protoc plugin
60+
if: steps.cache-plugin.outputs.cache-hit != 'true'
61+
working-directory: ./protoc-gen-rust-grpc
62+
shell: bash
63+
run: |
64+
set -e
65+
# On windows, the "//src" gets converted to "/". Disable this path
66+
# conversion.
67+
export MSYS_NO_PATHCONV=1
68+
export MSYS2_ARG_CONV_EXCL="*"
69+
70+
bazel build //src:protoc-gen-rust-grpc --enable_platform_specific_config
71+
72+
# The target path needs to match the cache config.
73+
TARGET_PATH="${{ runner.temp }}/protoc-plugin"
74+
mkdir -p "${TARGET_PATH}"
75+
cp bazel-bin/src/protoc-gen-rust-grpc "${TARGET_PATH}"
76+
2877
clippy:
2978
runs-on: ubuntu-latest
79+
needs: build-protoc-plugin
3080
steps:
3181
- uses: actions/checkout@v4
3282
- uses: hecrj/setup-rust-action@v2
3383
with:
3484
components: clippy
3585
- uses: taiki-e/install-action@protoc
86+
- name: Restore protoc plugin from cache
87+
id: cache-plugin
88+
uses: actions/cache@v4
89+
with:
90+
path: ${{ runner.temp }}/protoc-plugin
91+
key: ${{ runner.os }}-protoc-plugin-${{ hashFiles('protoc-gen-rust-grpc/src/**', 'protoc-gen-rust-grpc/.bazelrc', 'protoc-gen-rust-grpc/MODULE.bazel') }}
92+
- name: Add protoc plugin to PATH
93+
shell: bash
94+
run: |
95+
echo "${{ runner.temp }}/protoc-plugin" >> $GITHUB_PATH
3696
- uses: Swatinem/rust-cache@v2
3797
- run: cargo clippy --workspace --all-features --all-targets
3898

@@ -47,6 +107,7 @@ jobs:
47107

48108
udeps:
49109
runs-on: ubuntu-latest
110+
needs: build-protoc-plugin
50111
steps:
51112
- uses: actions/checkout@v4
52113
- uses: dtolnay/rust-toolchain@master
@@ -55,6 +116,16 @@ jobs:
55116
- uses: taiki-e/install-action@cargo-hack
56117
- uses: taiki-e/install-action@cargo-udeps
57118
- uses: taiki-e/install-action@protoc
119+
- name: Restore protoc plugin from cache
120+
id: cache-plugin
121+
uses: actions/cache@v4
122+
with:
123+
path: ${{ runner.temp }}/protoc-plugin
124+
key: ${{ runner.os }}-protoc-plugin-${{ hashFiles('protoc-gen-rust-grpc/src/**', 'protoc-gen-rust-grpc/.bazelrc', 'protoc-gen-rust-grpc/MODULE.bazel') }}
125+
- name: Add protoc plugin to PATH
126+
shell: bash
127+
run: |
128+
echo "${{ runner.temp }}/protoc-plugin" >> $GITHUB_PATH
58129
- uses: Swatinem/rust-cache@v2
59130
- run: cargo hack udeps --workspace --exclude-features=_tls-any,tls,tls-aws-lc,tls-ring --each-feature
60131
- run: cargo udeps --package tonic --features tls-ring,transport
@@ -66,6 +137,7 @@ jobs:
66137

67138
check:
68139
runs-on: ${{ matrix.os }}
140+
needs: build-protoc-plugin
69141
strategy:
70142
matrix:
71143
os: [ubuntu-latest, macOS-latest, windows-latest]
@@ -76,6 +148,16 @@ jobs:
76148
- uses: hecrj/setup-rust-action@v2
77149
- uses: taiki-e/install-action@cargo-hack
78150
- uses: taiki-e/install-action@protoc
151+
- name: Restore protoc plugin from cache
152+
id: cache-plugin
153+
uses: actions/cache@v4
154+
with:
155+
path: ${{ runner.temp }}/protoc-plugin
156+
key: ${{ runner.os }}-protoc-plugin-${{ hashFiles('protoc-gen-rust-grpc/src/**', 'protoc-gen-rust-grpc/.bazelrc', 'protoc-gen-rust-grpc/MODULE.bazel') }}
157+
- name: Add protoc plugin to PATH
158+
shell: bash
159+
run: |
160+
echo "${{ runner.temp }}/protoc-plugin" >> $GITHUB_PATH
79161
- uses: Swatinem/rust-cache@v2
80162
- name: Check features
81163
run: cargo hack check --workspace --no-private --each-feature --no-dev-deps
@@ -108,13 +190,24 @@ jobs:
108190

109191
test:
110192
runs-on: ${{ matrix.os }}
193+
needs: build-protoc-plugin
111194
strategy:
112195
matrix:
113196
os: [ubuntu-latest, macOS-latest, windows-latest]
114197
steps:
115198
- uses: actions/checkout@v4
116199
- uses: hecrj/setup-rust-action@v2
117200
- uses: taiki-e/install-action@protoc
201+
- name: Restore protoc plugin from cache
202+
id: cache-plugin
203+
uses: actions/cache@v4
204+
with:
205+
path: ${{ runner.temp }}/protoc-plugin
206+
key: ${{ runner.os }}-protoc-plugin-${{ hashFiles('protoc-gen-rust-grpc/src/**', 'protoc-gen-rust-grpc/.bazelrc', 'protoc-gen-rust-grpc/MODULE.bazel') }}
207+
- name: Add protoc plugin to PATH
208+
shell: bash
209+
run: |
210+
echo "${{ runner.temp }}/protoc-plugin" >> $GITHUB_PATH
118211
- uses: taiki-e/install-action@cargo-hack
119212
- uses: taiki-e/install-action@cargo-nextest
120213
- uses: Swatinem/rust-cache@v2
@@ -134,13 +227,24 @@ jobs:
134227
interop:
135228
name: Interop Tests
136229
runs-on: ${{ matrix.os }}
230+
needs: build-protoc-plugin
137231
strategy:
138232
matrix:
139233
os: [ubuntu-latest, macOS-latest, windows-latest]
140234
steps:
141235
- uses: actions/checkout@v4
142236
- uses: hecrj/setup-rust-action@v2
143237
- uses: taiki-e/install-action@protoc
238+
- name: Restore protoc plugin from cache
239+
id: cache-plugin
240+
uses: actions/cache@v4
241+
with:
242+
path: ${{ runner.temp }}/protoc-plugin
243+
key: ${{ runner.os }}-protoc-plugin-${{ hashFiles('protoc-gen-rust-grpc/src/**', 'protoc-gen-rust-grpc/.bazelrc', 'protoc-gen-rust-grpc/MODULE.bazel') }}
244+
- name: Add protoc plugin to PATH
245+
shell: bash
246+
run: |
247+
echo "${{ runner.temp }}/protoc-plugin" >> $GITHUB_PATH
144248
- uses: Swatinem/rust-cache@v2
145249
- name: Run interop tests
146250
run: ./interop/test.sh

Cargo.toml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ members = [
33
"tonic",
44
"tonic-build",
55
"tonic-health",
6+
"tonic-protobuf",
7+
"tonic-protobuf-build",
68
"tonic-types",
79
"tonic-reflection",
810
"tonic-prost",
@@ -13,24 +15,16 @@ members = [
1315
"grpc",
1416
"interop", # Tests
1517
"tests/disable_comments",
16-
"tests/included_service",
17-
"tests/same_name",
18-
"tests/service_named_service",
1918
"tests/wellknown",
2019
"tests/wellknown-compiled",
2120
"tests/extern_path/uuid",
22-
"tests/ambiguous_methods",
2321
"tests/extern_path/my_application",
2422
"tests/integration_tests",
25-
"tests/stream_conflict",
26-
"tests/root-crate-path",
2723
"tests/compression",
24+
"tests/compile",
2825
"tests/web",
29-
"tests/service_named_result",
30-
"tests/use_arc_self",
3126
"tests/default_stubs",
3227
"tests/deprecated_methods",
33-
"tests/skip_debug",
3428
]
3529
resolver = "2"
3630

grpc/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ hickory-resolver = { version = "0.25.1", optional = true }
3333
http = "1.1.0"
3434
http-body = "1.0.1"
3535
hyper = { version = "1.6.0", features = ["client", "http2"] }
36-
once_cell = "1.19.0"
3736
parking_lot = "0.12.4"
3837
pin-project-lite = "0.2.16"
3938
rand = "0.9"

grpc/src/client/load_balancing/registry.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use std::{
22
collections::HashMap,
3-
sync::{Arc, Mutex},
3+
sync::{Arc, LazyLock, Mutex},
44
};
55

6-
use once_cell::sync::Lazy;
7-
86
use super::LbPolicyBuilder;
97

108
/// A registry to store and retrieve LB policies. LB policies are indexed by
@@ -39,4 +37,4 @@ impl Default for LbPolicyRegistry {
3937

4038
/// The registry used if a local registry is not provided to a channel or if it
4139
/// does not exist in the local registry.
42-
pub static GLOBAL_LB_REGISTRY: Lazy<LbPolicyRegistry> = Lazy::new(LbPolicyRegistry::new);
40+
pub static GLOBAL_LB_REGISTRY: LazyLock<LbPolicyRegistry> = LazyLock::new(LbPolicyRegistry::new);

grpc/src/client/transport/registry.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use super::Transport;
2-
use once_cell::sync::Lazy;
3-
use std::{
4-
collections::HashMap,
5-
fmt::Debug,
6-
sync::{Arc, Mutex},
7-
};
2+
use std::sync::{Arc, LazyLock, Mutex};
3+
use std::{collections::HashMap, fmt::Debug};
84

95
/// A registry to store and retrieve transports. Transports are indexed by
106
/// the address type they are intended to handle.
@@ -52,4 +48,5 @@ impl TransportRegistry {
5248

5349
/// The registry used if a local registry is not provided to a channel or if it
5450
/// does not exist in the local registry.
55-
pub static GLOBAL_TRANSPORT_REGISTRY: Lazy<TransportRegistry> = Lazy::new(TransportRegistry::new);
51+
pub static GLOBAL_TRANSPORT_REGISTRY: LazyLock<TransportRegistry> =
52+
LazyLock::new(TransportRegistry::new);

grpc/src/inmemory/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
ops::Add,
44
sync::{
55
atomic::{AtomicU32, Ordering},
6-
Arc,
6+
Arc, LazyLock,
77
},
88
};
99

@@ -19,7 +19,6 @@ use crate::{
1919
server,
2020
service::{Request, Response, Service},
2121
};
22-
use once_cell::sync::Lazy;
2322
use tokio::sync::{mpsc, oneshot, Mutex, Notify};
2423
use tonic::async_trait;
2524

@@ -90,8 +89,8 @@ impl crate::server::Listener for Arc<Listener> {
9089
}
9190
}
9291

93-
static LISTENERS: Lazy<std::sync::Mutex<HashMap<String, Arc<Listener>>>> =
94-
Lazy::new(std::sync::Mutex::default);
92+
static LISTENERS: LazyLock<std::sync::Mutex<HashMap<String, Arc<Listener>>>> =
93+
LazyLock::new(std::sync::Mutex::default);
9594

9695
struct ClientTransport {}
9796

grpc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
pub mod client;
3535
pub mod credentials;
3636
pub mod inmemory;
37+
mod macros;
3738
pub mod rt;
3839
pub mod server;
3940
pub mod service;

grpc/src/macros.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
*
3+
* Copyright 2025 gRPC authors.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to
7+
* deal in the Software without restriction, including without limitation the
8+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
* sell copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
* IN THE SOFTWARE.
22+
*
23+
*/
24+
25+
/// Includes generated proto message, client, and server code.
26+
///
27+
/// You must specify the path to the `.proto` file
28+
/// **relative to the proto root directory**, without the `.proto` extension.
29+
///
30+
/// For example, if your proto directory is `path/to/protos` and it contains the
31+
/// file `helloworld.proto`, you would write:
32+
///
33+
/// ```rust,ignore
34+
/// mod pb {
35+
/// grpc::include_proto!("path/to/protos", "helloworld");
36+
/// }
37+
/// ```
38+
///
39+
/// # Note
40+
/// **This macro only works if the gRPC build output directory and message path
41+
/// are unmodified.**
42+
/// By default:
43+
/// - The output directory is set to the [`OUT_DIR`] environment variable.
44+
/// - The message path is set to `self`.
45+
///
46+
/// If your `.proto` files are not in a subdirectory, you can omit the first
47+
/// parameter.
48+
///
49+
/// ```rust,ignore
50+
/// mod pb {
51+
/// grpc::include_proto!("helloworld");
52+
/// }
53+
/// ```
54+
///
55+
/// If you have modified the output directory or message path, you should
56+
/// include the generated code manually instead of using this macro.
57+
///
58+
/// The following example assumes the message code is imported using `self`:
59+
///
60+
/// ```rust,ignore
61+
/// mod protos {
62+
/// // Include message code.
63+
/// include!("relative/protobuf/directory/generated.rs");
64+
///
65+
/// // Include service code.
66+
/// include!("relative/protobuf/directory/helloworld_grpc.pb.rs");
67+
/// }
68+
/// ```
69+
///
70+
/// If the message code and service code are in different modules, and the
71+
/// message path specified during code generation is `super::protos`, use:
72+
///
73+
/// ```rust,ignore
74+
/// mod protos {
75+
/// // Include message code.
76+
/// include!("relative/protobuf/directory/generated.rs");
77+
/// }
78+
///
79+
/// mod grpc {
80+
/// // Include service code.
81+
/// include!("relative/protobuf/directory/helloworld_grpc.pb.rs");
82+
/// }
83+
/// ```
84+
///
85+
/// [`OUT_DIR`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
86+
#[macro_export]
87+
macro_rules! include_proto {
88+
// Assume the generated output dir is OUT_DIR.
89+
($proto_file:literal) => {
90+
$crate::include_proto!("", $proto_file);
91+
};
92+
93+
($parent_dir:literal, $proto_file:literal) => {
94+
include!(concat!(env!("OUT_DIR"), "/", $parent_dir, "/generated.rs"));
95+
include!(concat!(
96+
env!("OUT_DIR"),
97+
"/",
98+
$parent_dir,
99+
"/",
100+
$proto_file,
101+
"_grpc.pb.rs"
102+
));
103+
};
104+
}

0 commit comments

Comments
 (0)