Skip to content

feat(http)!: report download progress through a callback - #48

Merged
dennis-tra merged 1 commit into
mainfrom
feat/download-progress-callback
Nov 13, 2026
Merged

dennis-tra merged 1 commit into
mainfrom
feat/download-progress-callback

Conversation

@dennis-tra

Copy link
Copy Markdown
Contributor

What and why

Client.Download logged its progress to a slog.Logger. The caller could change only the logger and the interval, so a tool that shows a progress bar or sets a metric had no access to the numbers.

This PR replaces the logger with a callback:

cfg := plhttp.DefaultDownloadConfig()
cfg.ProgressInterval = 100 * time.Millisecond
cfg.OnProgress = func(p plhttp.DownloadProgress) { bar.ChangeMax64(p.Total); bar.Set64(p.Done) }
  • OnProgress receives a DownloadProgress with URL, Offset, Done, Total and Elapsed.
  • Download calls it when the body starts, at most once per ProgressInterval while the body arrives, and once when the body is complete. A nil OnProgress means no reports.
  • A ProgressInterval of 0 is now valid: it reports after each read.
  • LogProgress(logger) logs the reports. A nil logger means slog.Default(). DefaultDownloadConfig() uses it, so callers of the default config still get progress logs.
  • The interval state stays inside Download, so one config can be shared by downloads that run at the same time.

Other designs I looked at: a callback on each read with throttle wrappers, an io.Writer, a channel, and atomic counters that the caller polls. The wrappers keep state in a closure, so a shared config mixes the counters of parallel downloads, and they cannot see the end of the body. An io.Writer gets no offset and no total, so a resumed download shows the wrong numbers.

How I checked it

  • just ci passes (gofumpt, go fix, vet, staticcheck, build, tests).
  • go test -race -run 'Download|Progress' ./http/ passes.
  • New tests:
    • TestClientDownloadReportsProgress checks the first and the last report of a new and of a resumed download.
    • TestProgressReaderInterval checks the interval with testing/synctest.
    • TestLogProgress checks the log lines.
  • I did not build amphion against this branch.

Deploy

Breaking change: DownloadConfig.Logger is removed. To log to your own logger, set cfg.OnProgress = plhttp.LogProgress(logger).

amphion (czds.go, tranco.go, commoncrawl.go) does not set Logger, so it needs no code change. After the update, each download logs one more line at the start (bytes=0) and one more at 100%.

Client.Download logged its progress to a slog.Logger, and the caller
could change only the logger and the interval. A caller that shows a
progress bar or sets a metric had no access to the numbers.

DownloadConfig.OnProgress now receives a DownloadProgress (URL, offset,
bytes done, total, elapsed time) when the body starts, at most once per
ProgressInterval, and when the body is complete. A ProgressInterval of
0 reports after each read. LogProgress gives the old log messages, and
DefaultDownloadConfig uses it, so callers of the default config keep
their progress logs.

BREAKING CHANGE: DownloadConfig.Logger is removed. To log the progress
to your own logger, set OnProgress to LogProgress(logger).
@dennis-tra
dennis-tra merged commit 1b0f941 into main Nov 13, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant