Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Playing a torrent while it downloads: how I got start time from 41 s to 8 s

Description and use

Using anacrolix/torrent’s responsive reader and a three‑minute download window reduced median start time from 41 s to 8.3 s and seek time from 43 s to 3.8 s, meeting the 15‑second start‑time goal.

Problem

Streaming video from a torrent that is still downloading often stalls because the player requests bytes that are not yet available, especially the start and end of the file.

Outcome

Using anacrolix/torrent’s responsive reader and a three‑minute download window reduced median start time from 41 s to 8.3 s and seek time from 43 s to 3.8 s, meeting the 15‑second start‑time goal.

Workflow

  • 1. Measure start and seek times by downloading first 2 MB and last 1 MB, then middle 2 MB and 85% 2 MB.
  • 2. Test with Rust library – record median times.
  • 3. Replace with Go service using anacrolix/torrent and enable responsive reader with readahead 16 MiB.
  • 4. Serve HTTP range requests via http.ServeContent, setting Content‑Type manually.
  • 5. Implement a three‑minute download window ahead of playback, downloading in 64 MiB batches.
  • 6. Remove prewarm requests that read start and end immediately after torrent addition.
  • 7. Measure and compare results.
  • 8. Handle library callbacks and panics to avoid deadlocks and lock leaks.

Evidence

Median start: 41 seconds. Worse, it was random. The same file started in 2 seconds on one run and 57 on the next.

Source

I swapped it for a small Go service on top of anacrolix/torrent. The library handles the hard stuff (peers, DHT, picking pieces), and my code is just the HTTP API, the stream, file selection and storage.

Source

For Range requests I let http.ServeContent do the work: 206, 416, Content-Range, Content-Length, all handled.

Source

So while you watch, only a window ahead of the position gets downloaded: roughly three minutes of video, worked out from the file's average bitrate and kept between 64 MB and 1 GiB.

Source

The prewarm requests were fighting the player's own requests for the same peers. I took it out, and it stays out until a benchmark says otherwise.

Source

Library callbacks run under its lock. anacrolix calls some callbacks while it holds the client lock. Touch the client from inside one, and the engine deadlocks.

Source

  • Record date: 2026-11-02T00:14:09.724Z