Skip to content

Commit

Permalink
Make flush threshold configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Reidel <[email protected]>
  • Loading branch information
Gelbpunkt committed Sep 10, 2024
1 parent 4781150 commit 2869e8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/proto/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ where
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
// tokio-util calls poll_flush when more than 8096 bytes are pending, otherwise
// it returns Ready. We will just replicate that behavior
if self.pending_bytes >= 8096 {
if self.pending_bytes >= self.inner.decoder().limits.flush_threshold {
self.as_mut().poll_flush(cx)
} else {
Poll::Ready(Ok(()))
Expand Down
15 changes: 15 additions & 0 deletions src/proto/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ pub struct Limits {
/// The maximum allowed payload length. The default
/// is 64 MiB.
pub(super) max_payload_len: usize,
/// Threshold of queued up bytes after which the underlying I/O is flushed
/// before the sink is declared ready. The default is 8 KiB.
pub(super) flush_threshold: usize,
}

impl Limits {
Expand All @@ -571,6 +574,7 @@ impl Limits {
pub fn unlimited() -> Self {
Self {
max_payload_len: usize::MAX,
flush_threshold: usize::MAX,
}
}

Expand All @@ -582,12 +586,23 @@ impl Limits {

self
}

/// Sets the threshold of queued up bytes after which the underlying I/O is
/// flushed before the sink is declared ready. `None` equals no limit. The
/// default is 8 KiB.
#[must_use]
pub fn flush_threshold(mut self, threshold: Option<usize>) -> Self {
self.flush_threshold = threshold.unwrap_or(usize::MAX);

self
}
}

impl Default for Limits {
fn default() -> Self {
Self {
max_payload_len: 64 * 1024 * 1024,
flush_threshold: 8 * 1024,
}
}
}
Expand Down

0 comments on commit 2869e8a

Please sign in to comment.