Skip to content

Commit 0218d58

Browse files
JohnDonethLucioFranco
authored andcommitted
feat(transport): Expose http/2 settings (#28)
* expose http2 settings * add client http2 options
1 parent afa9d9d commit 0218d58

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

tonic/src/transport/endpoint.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub struct Endpoint {
2424
pub(super) buffer_size: Option<usize>,
2525
pub(super) interceptor_headers:
2626
Option<Arc<dyn Fn(&mut http::HeaderMap) + Send + Sync + 'static>>,
27+
pub(super) init_stream_window_size: Option<u32>,
28+
pub(super) init_connection_window_size: Option<u32>,
2729
}
2830

2931
impl Endpoint {
@@ -101,6 +103,25 @@ impl Endpoint {
101103
self
102104
}
103105

106+
/// Sets the [`SETTINGS_INITIAL_WINDOW_SIZE`][spec] option for HTTP2
107+
/// stream-level flow control.
108+
///
109+
/// Default is 65,535
110+
///
111+
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_INITIAL_WINDOW_SIZE
112+
pub fn initial_stream_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
113+
self.init_stream_window_size = sz.into();
114+
self
115+
}
116+
117+
/// Sets the max connection-level flow control for HTTP2
118+
///
119+
/// Default is 65,535
120+
pub fn initial_connection_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
121+
self.init_connection_window_size = sz.into();
122+
self
123+
}
124+
104125
/// Enable TLS and apply the CA as the root certificate.
105126
///
106127
/// Providing an optional domain to override. If `None` is passed to this
@@ -185,6 +206,8 @@ impl From<Uri> for Endpoint {
185206
tls: None,
186207
buffer_size: None,
187208
interceptor_headers: None,
209+
init_stream_window_size: None,
210+
init_connection_window_size: None,
188211
}
189212
}
190213
}

tonic/src/transport/server.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub struct Server {
4747
// timeout: Option<Duration>,
4848
#[cfg(feature = "tls")]
4949
tls: Option<TlsAcceptor>,
50+
init_stream_window_size: Option<u32>,
51+
init_connection_window_size: Option<u32>,
52+
max_concurrent_streams: Option<u32>,
5053
}
5154

5255
impl Server {
@@ -123,6 +126,36 @@ impl Server {
123126
// self
124127
// }
125128

129+
/// Sets the [`SETTINGS_INITIAL_WINDOW_SIZE`][spec] option for HTTP2
130+
/// stream-level flow control.
131+
///
132+
/// Default is 65,535
133+
///
134+
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_INITIAL_WINDOW_SIZE
135+
pub fn initial_stream_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
136+
self.init_stream_window_size = sz.into();
137+
self
138+
}
139+
140+
/// Sets the max connection-level flow control for HTTP2
141+
///
142+
/// Default is 65,535
143+
pub fn initial_connection_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
144+
self.init_connection_window_size = sz.into();
145+
self
146+
}
147+
148+
/// Sets the [`SETTINGS_MAX_CONCURRENT_STREAMS`][spec] option for HTTP2
149+
/// connections.
150+
///
151+
/// Default is no limit (`None`).
152+
///
153+
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_MAX_CONCURRENT_STREAMS
154+
pub fn max_concurrent_streams(&mut self, max: impl Into<Option<u32>>) -> &mut Self {
155+
self.max_concurrent_streams = max.into();
156+
self
157+
}
158+
126159
/// Intercept the execution of gRPC methods.
127160
///
128161
/// ```
@@ -162,6 +195,9 @@ impl Server {
162195
{
163196
let interceptor = self.interceptor.clone();
164197
let concurrency_limit = self.concurrency_limit;
198+
let init_connection_window_size = self.init_connection_window_size;
199+
let init_stream_window_size = self.init_stream_window_size;
200+
let max_concurrent_streams = self.max_concurrent_streams;
165201
// let timeout = self.timeout.clone();
166202

167203
let incoming = hyper::server::accept::from_stream(async_stream::try_stream! {
@@ -190,6 +226,9 @@ impl Server {
190226

191227
hyper::Server::builder(incoming)
192228
.http2_only(true)
229+
.http2_initial_connection_window_size(init_connection_window_size)
230+
.http2_initial_stream_window_size(init_stream_window_size)
231+
.http2_max_concurrent_streams(max_concurrent_streams)
193232
.serve(svc)
194233
.await
195234
.map_err(map_err)?;

tonic/src/transport/service/connection.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ impl Connection {
3434
#[cfg(not(feature = "tls"))]
3535
let connector = connector();
3636

37-
let settings = Builder::new().http2_only(true).clone();
37+
let settings = Builder::new()
38+
.http2_initial_stream_window_size(endpoint.init_stream_window_size)
39+
.http2_initial_connection_window_size(endpoint.init_connection_window_size)
40+
.http2_only(true)
41+
.clone();
3842

3943
let stack = ServiceBuilder::new()
4044
.layer_fn(|s| AddOrigin::new(s, endpoint.uri.clone()))

0 commit comments

Comments
 (0)