From e4aac7930fc578fd3f128ab3d4e7b2e1c0135577 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 19 Aug 2024 13:40:01 +0100 Subject: [PATCH] make async-backtrace optional --- crates/librqbit/Cargo.toml | 3 +- crates/librqbit/src/lib.rs | 13 +++++++ crates/librqbit/src/tests/test_util.rs | 12 +++++- crates/librqbit/src/torrent_state/live/mod.rs | 37 +++++++++---------- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/crates/librqbit/Cargo.toml b/crates/librqbit/Cargo.toml index 6ddab8b..8d45a1d 100644 --- a/crates/librqbit/Cargo.toml +++ b/crates/librqbit/Cargo.toml @@ -22,6 +22,7 @@ storage_middleware = ["lru"] storage_examples = [] tracing-subscriber-utils = ["tracing-subscriber"] postgres = ["sqlx"] +async-bt = ["async-backtrace"] [dependencies] sqlx = { version = "0.7", features = [ @@ -88,7 +89,7 @@ lru = { version = "0.12.3", optional = true } mime_guess = { version = "2.0.5", default-features = false } tokio-socks = "0.5.2" async-trait = "0.1.81" -async-backtrace = "0.2" +async-backtrace = { version = "0.2", optional = true } [build-dependencies] anyhow = "1" diff --git a/crates/librqbit/src/lib.rs b/crates/librqbit/src/lib.rs index e6a5a27..16ea5be 100644 --- a/crates/librqbit/src/lib.rs +++ b/crates/librqbit/src/lib.rs @@ -25,6 +25,19 @@ #![warn(clippy::cast_possible_truncation)] +macro_rules! aframe { + ($e:expr) => {{ + #[cfg(feature = "async-bt")] + { + async_backtrace::frame!($e) + } + #[cfg(not(feature = "async-bt"))] + { + $e + } + }}; +} + pub mod api; mod api_error; mod chunk_tracker; diff --git a/crates/librqbit/src/tests/test_util.rs b/crates/librqbit/src/tests/test_util.rs index 1ffe9df..4f4dd3b 100644 --- a/crates/librqbit/src/tests/test_util.rs +++ b/crates/librqbit/src/tests/test_util.rs @@ -85,7 +85,17 @@ impl TestPeerMetadata { async fn debug_server() -> anyhow::Result<()> { async fn backtraces() -> impl IntoResponse { - async_backtrace::taskdump_tree(true) + #[cfg(feature = "async-bt")] + { + async_backtrace::taskdump_tree(true) + } + #[cfg(not(feature = "async-bt"))] + { + use crate::ApiError; + ApiError::from(anyhow::anyhow!( + "backtraces not enabled, enable async-bt feature" + )) + } } let app = Router::new().route("/backtrace", get(backtraces)); diff --git a/crates/librqbit/src/torrent_state/live/mod.rs b/crates/librqbit/src/torrent_state/live/mod.rs index ed9f0fc..6bbb2a9 100644 --- a/crates/librqbit/src/torrent_state/live/mod.rs +++ b/crates/librqbit/src/torrent_state/live/mod.rs @@ -70,12 +70,9 @@ use peer_binary_protocol::{ extended::{handshake::ExtendedHandshake, ut_metadata::UtMetadata, ExtendedMessage}, Handshake, Message, MessageOwned, Piece, Request, }; -use tokio::{ - sync::{ - mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}, - Notify, OwnedSemaphorePermit, Semaphore, - }, - time::timeout, +use tokio::sync::{ + mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}, + Notify, OwnedSemaphorePermit, Semaphore, }; use tokio_util::sync::CancellationToken; use tracing::{debug, error, error_span, info, trace, warn, Instrument}; @@ -349,13 +346,13 @@ impl TorrentStateLive { "manage_incoming_peer", addr = %checked_peer.addr ), - self.clone() - .task_manage_incoming_peer(checked_peer, counters, tx, rx, permit), + aframe!(self + .clone() + .task_manage_incoming_peer(checked_peer, counters, tx, rx, permit)), ); Ok(()) } - #[async_backtrace::framed] async fn task_manage_incoming_peer( self: Arc, checked_peer: CheckedIncomingConnection, @@ -416,7 +413,6 @@ impl TorrentStateLive { Ok(()) } - #[async_backtrace::framed] async fn task_manage_outgoing_peer( self: Arc, addr: SocketAddr, @@ -453,10 +449,10 @@ impl TorrentStateLive { state.meta.spawner, state.meta.connector.clone(), ); - let requester = async_backtrace::frame!(handler + let requester = aframe!(handler .task_peer_chunk_requester() .instrument(error_span!("chunk_requester"))); - let conn_manager = async_backtrace::frame!(peer_connection + let conn_manager = aframe!(peer_connection .manage_peer_outgoing(rx, state.have_broadcast_tx.subscribe()) .instrument(error_span!("peer_connection"))); @@ -499,7 +495,7 @@ impl TorrentStateLive { let permit = state.peer_semaphore.clone().acquire_owned().await?; state.spawn( error_span!(parent: state.meta.span.clone(), "manage_peer", peer = addr.to_string()), - state.clone().task_manage_outgoing_peer(addr, permit), + aframe!(state.clone().task_manage_outgoing_peer(addr, permit)), ); } } @@ -1218,7 +1214,7 @@ impl PeerHandler { } loop { - async_backtrace::frame!(self.wait_for_unchoke()).await; + aframe!(self.wait_for_unchoke()).await; if self.state.is_finished_and_dont_need_peers() { debug!("nothing left to do, disconnecting peer"); @@ -1238,12 +1234,15 @@ impl PeerHandler { Some(next) => next, None => { debug!("no pieces to request"); - let _ = async_backtrace::frame!(tokio::time::timeout( + match aframe!(tokio::time::timeout( Duration::from_secs(10), new_piece_notify )) - .await; - debug!("woken up"); + .await + { + Ok(()) => debug!("woken up, new pieces might be available"), + Err(_) => debug!("woken up by sleep timer"), + } continue; } }; @@ -1277,9 +1276,9 @@ impl PeerHandler { }; loop { - match async_backtrace::frame!(timeout( + match aframe!(tokio::time::timeout( Duration::from_secs(10), - async_backtrace::frame!(self.requests_sem.acquire()) + aframe!(self.requests_sem.acquire()) )) .await {