Skip to content

Commit ab71f8c

Browse files
committed
feat(server): add http2_max_concurrent_streams builder option
Closes #1772
1 parent 3a6080b commit ab71f8c

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

Diff for: src/proto/h2/server.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ where
5050
B: Payload,
5151
E: H2Exec<S::Future, B>,
5252
{
53-
pub(crate) fn new(io: T, service: S, exec: E) -> Server<T, S, B, E> {
54-
let handshake = Builder::new()
55-
.handshake(io);
53+
pub(crate) fn new(io: T, service: S, builder: &Builder, exec: E) -> Server<T, S, B, E> {
54+
let handshake = builder.handshake(io);
5655
Server {
5756
exec,
5857
state: State::Handshaking(handshake),

Diff for: src/server/conn.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::sync::Arc;
1717
use bytes::Bytes;
1818
use futures::{Async, Future, Poll, Stream};
1919
use futures::future::{Either, Executor};
20+
use h2;
2021
use tokio_io::{AsyncRead, AsyncWrite};
2122
#[cfg(feature = "runtime")] use tokio_reactor::Handle;
2223

@@ -46,6 +47,7 @@ pub struct Http<E = Exec> {
4647
exec: E,
4748
h1_half_close: bool,
4849
h1_writev: bool,
50+
h2_builder: h2::server::Builder,
4951
mode: ConnectionMode,
5052
keep_alive: bool,
5153
max_buf_size: Option<usize>,
@@ -120,14 +122,14 @@ where
120122

121123
#[derive(Clone, Debug)]
122124
enum Fallback<E> {
123-
ToHttp2(E),
125+
ToHttp2(h2::server::Builder, E),
124126
Http1Only,
125127
}
126128

127129
impl<E> Fallback<E> {
128130
fn to_h2(&self) -> bool {
129131
match *self {
130-
Fallback::ToHttp2(_) => true,
132+
Fallback::ToHttp2(..) => true,
131133
Fallback::Http1Only => false,
132134
}
133135
}
@@ -165,6 +167,7 @@ impl Http {
165167
exec: Exec::Default,
166168
h1_half_close: true,
167169
h1_writev: true,
170+
h2_builder: h2::server::Builder::default(),
168171
mode: ConnectionMode::Fallback,
169172
keep_alive: true,
170173
max_buf_size: None,
@@ -236,6 +239,19 @@ impl<E> Http<E> {
236239
self
237240
}
238241

242+
/// Sets the [`SETTINGS_MAX_CONCURRENT_STREAMS`][spec] option for HTTP2
243+
/// connections.
244+
///
245+
/// Default is no limit (`None`).
246+
///
247+
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_MAX_CONCURRENT_STREAMS
248+
pub fn http2_max_concurrent_streams(&mut self, max: impl Into<Option<u32>>) -> &mut Self {
249+
if let Some(max) = max.into() {
250+
self.h2_builder.max_concurrent_streams(max);
251+
}
252+
self
253+
}
254+
239255
/// Enables or disables HTTP keep-alive.
240256
///
241257
/// Default is true.
@@ -278,6 +294,7 @@ impl<E> Http<E> {
278294
exec,
279295
h1_half_close: self.h1_half_close,
280296
h1_writev: self.h1_writev,
297+
h2_builder: self.h2_builder,
281298
mode: self.mode,
282299
keep_alive: self.keep_alive,
283300
max_buf_size: self.max_buf_size,
@@ -350,15 +367,15 @@ impl<E> Http<E> {
350367
}
351368
ConnectionMode::H2Only => {
352369
let rewind_io = Rewind::new(io);
353-
let h2 = proto::h2::Server::new(rewind_io, service, self.exec.clone());
370+
let h2 = proto::h2::Server::new(rewind_io, service, &self.h2_builder, self.exec.clone());
354371
Either::B(h2)
355372
}
356373
};
357374

358375
Connection {
359376
conn: Some(either),
360377
fallback: if self.mode == ConnectionMode::Fallback {
361-
Fallback::ToHttp2(self.exec.clone())
378+
Fallback::ToHttp2(self.h2_builder.clone(), self.exec.clone())
362379
} else {
363380
Fallback::Http1Only
364381
},
@@ -538,11 +555,16 @@ where
538555
};
539556
let mut rewind_io = Rewind::new(io);
540557
rewind_io.rewind(read_buf);
541-
let exec = match self.fallback {
542-
Fallback::ToHttp2(ref exec) => exec.clone(),
558+
let (builder, exec) = match self.fallback {
559+
Fallback::ToHttp2(ref builder, ref exec) => (builder, exec),
543560
Fallback::Http1Only => unreachable!("upgrade_h2 with Fallback::Http1Only"),
544561
};
545-
let h2 = proto::h2::Server::new(rewind_io, dispatch.into_service(), exec);
562+
let h2 = proto::h2::Server::new(
563+
rewind_io,
564+
dispatch.into_service(),
565+
builder,
566+
exec.clone(),
567+
);
546568

547569
debug_assert!(self.conn.is_none());
548570
self.conn = Some(Either::B(h2));

Diff for: src/server/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,17 @@ impl<I, E> Builder<I, E> {
302302
self
303303
}
304304

305+
/// Sets the [`SETTINGS_MAX_CONCURRENT_STREAMS`][spec] option for HTTP2
306+
/// connections.
307+
///
308+
/// Default is no limit (`None`).
309+
///
310+
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_MAX_CONCURRENT_STREAMS
311+
pub fn http2_max_concurrent_streams(mut self, max: impl Into<Option<u32>>) -> Self {
312+
self.protocol.http2_max_concurrent_streams(max.into());
313+
self
314+
}
315+
305316
/// Set the maximum buffer size.
306317
///
307318
/// Default is ~ 400kb.

0 commit comments

Comments
 (0)