From 1c9aa8ca72ea3586990e6f094a7be80d82634e49 Mon Sep 17 00:00:00 2001 From: Ivan Date: Sat, 20 Jul 2024 20:03:14 +0200 Subject: [PATCH] Fist naive attempt to improve streaming at leat inform executor about blocking call --- crates/librqbit/src/http_api.rs | 2 +- .../librqbit/src/torrent_state/streaming.rs | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index 57c4d8b..dbd8274 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -170,7 +170,7 @@ impl HttpApi { let range_header = headers.get(http::header::RANGE); trace!(torrent_id=idx, file_id=file_id, range=?range_header, "request for HTTP stream"); - if let Some(range) = headers.get(http::header::RANGE) { + if let Some(range) = range_header { let offset: Option = range .to_str() .ok() diff --git a/crates/librqbit/src/torrent_state/streaming.rs b/crates/librqbit/src/torrent_state/streaming.rs index 48b4b3e..e96e966 100644 --- a/crates/librqbit/src/torrent_state/streaming.rs +++ b/crates/librqbit/src/torrent_state/streaming.rs @@ -12,7 +12,10 @@ use anyhow::Context; use dashmap::DashMap; use librqbit_core::lengths::{CurrentPiece, Lengths, ValidPieceIndex}; -use tokio::io::{AsyncRead, AsyncSeek}; +use tokio::{ + io::{AsyncRead, AsyncSeek}, + task::block_in_place, +}; use tracing::{debug, trace}; use crate::{file_info::FileInfo, storage::TorrentStorage, ManagedTorrent}; @@ -211,13 +214,13 @@ impl AsyncRead for FileStream { "will write bytes" ); - poll_try_io!(poll_try_io!(self.torrent.with_storage_and_file( - self.file_id, - |files, _fi| { - files.pread_exact(self.file_id, self.position, buf)?; - Ok::<_, anyhow::Error>(()) - } - ))); + poll_try_io!(poll_try_io!(block_in_place(|| { + self.torrent + .with_storage_and_file(self.file_id, |files, _fi| { + files.pread_exact(self.file_id, self.position, buf)?; + Ok::<_, anyhow::Error>(()) + }) + }))); self.as_mut().advance(bytes_to_read as u64); tbuf.advance(bytes_to_read);