From be4589797b273d82083613cc8c26459f81389a15 Mon Sep 17 00:00:00 2001 From: gridoe Date: Tue, 17 Nov 2026 14:42:22 +0200 Subject: [PATCH] fix: stream hosts get 6 s to answer before refryt tries a mirror The first-byte probe and each proxy range now share one deadline, 6 s, up from 5 s, so a slow but working connection has more room. A reachable host answered in 0.06-0.75 s in testing. mpv's curl-connect-timeout follows it; it only exists on mpv built with libcurl (0.42 and our Windows libmpv), where it defaults to 30 s per dead host. The README line about mirrors is dropped. --- README.md | 2 -- crates/refryt-resolve/src/lib.rs | 8 +++++++- crates/refryt-resolve/src/proxy.rs | 12 +++++++++--- crates/refryt/src/player.rs | 6 ++++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a49fbfc..672120d 100644 --- a/README.md +++ b/README.md @@ -207,8 +207,6 @@ There is no telemetry. refryt connects to YouTube, [LRCLIB](https://lrclib.net) The project is before 1.0 and changes quickly. Only the Linux builds see daily use; the Windows builds are cross-compiled with limited testing on real hardware, and the macOS build has been tested by hand. Podcasts are not supported. A change to YouTube's internal API can break playback or a page until a fix is released. -If an assigned YouTube stream host is unreachable, refryt retries alternate nodes listed in the stream URL. - Bugs go to the [issue tracker](https://github.com/gridoe/refryt/issues); [CONTRIBUTING.md](CONTRIBUTING.md) says what to include. ## Build from source diff --git a/crates/refryt-resolve/src/lib.rs b/crates/refryt-resolve/src/lib.rs index a5d4fd9..333d153 100644 --- a/crates/refryt-resolve/src/lib.rs +++ b/crates/refryt-resolve/src/lib.rs @@ -296,6 +296,12 @@ fn mirror_url(url: &Url, node: &str) -> Option { Some(mirrored) } +/// How long a stream host gets to send its response headers, connection included, before +/// it counts as unreachable and a mirror is tried: for [`probe_first_byte`] and for each +/// range the proxy fetches. A reachable host answers in well under a second (0.06–0.75 s +/// measured 2026-11-17, cold), and a dead one should cost little more than this. +pub(crate) const STREAM_DEADLINE: Duration = Duration::from_secs(6); + /// Ask a stream host for its first byte, and fail over to a mirror node when it will not /// take the connection. /// @@ -315,7 +321,7 @@ pub async fn probe_first_byte(http: &reqwest::Client, url: &mut Url) -> Result<( .header("User-Agent", guest::GUEST_USER_AGENT) // Short, so a dead node costs seconds: the mirrors are tried while the caller // is still waiting for a URL worth playing. - .timeout(Duration::from_secs(5)) + .timeout(STREAM_DEADLINE) .send() }; let response = match get(url).await { diff --git a/crates/refryt-resolve/src/proxy.rs b/crates/refryt-resolve/src/proxy.rs index 97d6191..5c99e93 100644 --- a/crates/refryt-resolve/src/proxy.rs +++ b/crates/refryt-resolve/src/proxy.rs @@ -427,11 +427,12 @@ async fn fetch_range( end: u64, ) -> Result> { // Bound each attempt through response headers, including DNS/TCP/TLS and pooled - // connections. Leave body reads unbounded: a healthy 1 MiB range can take over 5 s. + // connections. Leave body reads unbounded: a healthy 1 MiB range can take longer + // than the deadline. let get = |url: Url| async move { Ok::<_, anyhow::Error>( tokio::time::timeout( - std::time::Duration::from_secs(5), + crate::STREAM_DEADLINE, http.get(url) .header("Range", format!("bytes={start}-{end}")) .header("User-Agent", GUEST_USER_AGENT) @@ -439,7 +440,12 @@ async fn fetch_range( .send(), ) .await - .context("the stream host did not answer within 5 seconds")??, + .with_context(|| { + format!( + "the stream host did not answer within {} seconds", + crate::STREAM_DEADLINE.as_secs() + ) + })??, ) }; let url = entry.url.lock().unwrap().clone(); diff --git a/crates/refryt/src/player.rs b/crates/refryt/src/player.rs index 460bc0a..df3d7df 100644 --- a/crates/refryt/src/player.rs +++ b/crates/refryt/src/player.rs @@ -92,8 +92,10 @@ impl Player { // its own edge. 4 MiB is a few minutes of Opus — plenty for a scrub-back. init.set_property("demuxer-max-back-bytes", "4MiB")?; // Bound connection establishment only; body reads keep their normal tolerance - // for buffered proxy streams. Older mpv builds without curl lack this option. - let _ = init.set_property("curl-connect-timeout", "5"); + // for buffered proxy streams. Only mpv built with libcurl has this option (0.42 + // and later, and the Windows libmpv); its default is 30 s per dead stream host. + // Others ignore it. It only matters for URLs mpv opens itself, not the proxy's. + let _ = init.set_property("curl-connect-timeout", "6"); // We resolve stream URLs ourselves via refryt-resolve, so mpv must not invoke // yt-dlp for them — direct `googlevideo.com` URLs never match ytdl_hook's // `youtube.com` patterns, so enabling the hook costs nothing there. The in-app