Skip to content

Commit 078ed82

Browse files
almielczarekseanmonstar
authored andcommitted
feat(client): add conn::Builder::max_buf_size()
This allows users to configure a limit to client connections' read and write buffers. Closes #1748
1 parent 4dd9437 commit 078ed82

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/client/conn.rs

+18
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub struct Builder {
7575
h1_writev: bool,
7676
h1_title_case_headers: bool,
7777
h1_read_buf_exact_size: Option<usize>,
78+
h1_max_buf_size: Option<usize>,
7879
http2: bool,
7980
}
8081

@@ -435,6 +436,7 @@ impl Builder {
435436
h1_writev: true,
436437
h1_read_buf_exact_size: None,
437438
h1_title_case_headers: false,
439+
h1_max_buf_size: None,
438440
http2: false,
439441
}
440442
}
@@ -460,8 +462,21 @@ impl Builder {
460462

461463
pub(super) fn h1_read_buf_exact_size(&mut self, sz: Option<usize>) -> &mut Builder {
462464
self.h1_read_buf_exact_size = sz;
465+
self.h1_max_buf_size = None;
463466
self
464467
}
468+
469+
pub(super) fn h1_max_buf_size(&mut self, max: usize) -> &mut Self {
470+
assert!(
471+
max >= proto::h1::MINIMUM_MAX_BUFFER_SIZE,
472+
"the max_buf_size cannot be smaller than the minimum that h1 specifies."
473+
);
474+
475+
self.h1_max_buf_size = Some(max);
476+
self.h1_read_buf_exact_size = None;
477+
self
478+
}
479+
465480
/// Sets whether HTTP2 is required.
466481
///
467482
/// Default is false.
@@ -510,6 +525,9 @@ where
510525
if let Some(sz) = self.builder.h1_read_buf_exact_size {
511526
conn.set_read_buf_exact_size(sz);
512527
}
528+
if let Some(max) = self.builder.h1_max_buf_size {
529+
conn.set_max_buf_size(max);
530+
}
513531
let cd = proto::h1::dispatch::Client::new(rx);
514532
let dispatch = proto::h1::Dispatcher::new(cd, conn);
515533
Either::A(dispatch)

src/client/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -901,13 +901,30 @@ impl Builder {
901901

902902
/// Sets the exact size of the read buffer to *always* use.
903903
///
904+
/// Note that setting this option unsets the `http1_max_buf_size` option.
905+
///
904906
/// Default is an adaptive read buffer.
905907
#[inline]
906908
pub fn http1_read_buf_exact_size(&mut self, sz: usize) -> &mut Self {
907909
self.conn_builder.h1_read_buf_exact_size(Some(sz));
908910
self
909911
}
910912

913+
/// Set the maximum buffer size for the connection.
914+
///
915+
/// Default is ~400kb.
916+
///
917+
/// Note that setting this option unsets the `http1_read_exact_buf_size` option.
918+
///
919+
/// # Panics
920+
///
921+
/// The minimum value allowed is 8192. This method panics if the passed `max` is less than the minimum.
922+
#[inline]
923+
pub fn http1_max_buf_size(&mut self, max: usize) -> &mut Self {
924+
self.conn_builder.h1_max_buf_size(max);
925+
self
926+
}
927+
911928
/// Set whether HTTP/1 connections will write header names as title case at
912929
/// the socket level.
913930
///

0 commit comments

Comments
 (0)