public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [RFC PATCH proxmox-backup 1/2] api: optimize content streaming reader for file download
Date: Mon, 26 Oct 2026 14:45:58 +0100	[thread overview]
Message-ID: <20261026134559.352926-2-c.ebner@proxmox.com> (raw)
In-Reply-To: <20261026134559.352926-1-c.ebner@proxmox.com>

The current implementation using FramedRead and BytesCodec reads the
file in 8K chunks only. FramedRead starts out with a default buffer
capacity of 8K [0], the reader filling only up to this capacity [1],
which BytesCodec::decode() consumes completely [2]. Therefore, the
next read will recreate the same 8K buffer again because
BytesMut::reserve() [3] will fill to the initial capacity again.

This has additional overhead since tokio's async reader uses the
blocking thread pool for each read and is especially problematic if
the blocking pool is heavily utilized, e.g. by concurrent operations
such as chunk inserts. Latter also run on the blocking pool since
commit 23400016 ("api: do not block tokio worker threads during chunk
inserts").

Use the ReaderStream with tokio's default maximum buffer size for
blocking I/O instead [4].

[0] https://docs.rs/tokio-util/0.7.10/src/tokio_util/codec/framed_impl.rs.html#53
[1] https://docs.rs/tokio-util/0.7.10/src/tokio_util/codec/framed_impl.rs.html#221
[2] https://docs.rs/tokio-util/0.7.10/src/tokio_util/codec/bytes_codec.rs.html#58
[3] https://docs.rs/tokio-util/0.7.10/src/tokio_util/codec/framed_impl.rs.html#221
[4] https://docs.rs/tokio/1.54.1/src/tokio/io/blocking.rs.html#29

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
 src/api2/helpers.rs | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/api2/helpers.rs b/src/api2/helpers.rs
index aa582a53c..7d1fb8e35 100644
--- a/src/api2/helpers.rs
+++ b/src/api2/helpers.rs
@@ -1,12 +1,14 @@
 use std::path::PathBuf;
 
 use anyhow::Error;
-use futures::stream::TryStreamExt;
 use hyper::{Response, StatusCode, header};
+use tokio_util::io::ReaderStream;
 
 use proxmox_http::Body;
 use proxmox_router::http_bail;
 
+const DOWNLOAD_BUFFER_SIZE: usize = 2 * 1024 * 1024;
+
 pub async fn create_download_response(path: PathBuf) -> Result<Response<Body>, Error> {
     let file = match tokio::fs::File::open(path.clone()).await {
         Ok(file) => file,
@@ -16,9 +18,7 @@ pub async fn create_download_response(path: PathBuf) -> Result<Response<Body>, E
         Err(err) => http_bail!(BAD_REQUEST, "open file {:?} failed: {}", path, err),
     };
 
-    let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
-        .map_ok(|bytes| bytes.freeze());
-
+    let payload = ReaderStream::with_capacity(file, DOWNLOAD_BUFFER_SIZE);
     let body = Body::wrap_stream(payload);
 
     // fixme: set other headers ?
-- 
2.47.3





  reply	other threads:[~2026-10-26 13:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26 13:45 [RFC PATCH proxmox-backup 0/2] fix #7756: avoid qmp 'backup' timeout Christian Ebner
2026-10-26 13:45 ` Christian Ebner [this message]
2026-10-26 13:45 ` [RFC PATCH proxmox-backup 2/2] fix #7756: api: stream file contents via dedicated thread Christian Ebner
2026-10-27 10:41 ` [RFC PATCH proxmox-backup 0/2] fix #7756: avoid qmp 'backup' timeout Christian Ebner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261026134559.352926-2-c.ebner@proxmox.com \
    --to=c.ebner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal