Repository navigation
feat(http)!: report download progress through a callback - #48
Merged
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
Client.Downloadlogged its progress to aslog.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:
OnProgressreceives aDownloadProgresswithURL,Offset,Done,TotalandElapsed.Downloadcalls it when the body starts, at most once perProgressIntervalwhile the body arrives, and once when the body is complete. A nilOnProgressmeans no reports.ProgressIntervalof 0 is now valid: it reports after each read.LogProgress(logger)logs the reports. A nil logger meansslog.Default().DefaultDownloadConfig()uses it, so callers of the default config still get progress logs.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. Anio.Writergets no offset and no total, so a resumed download shows the wrong numbers.How I checked it
just cipasses (gofumpt,go fix, vet, staticcheck, build, tests).go test -race -run 'Download|Progress' ./http/passes.TestClientDownloadReportsProgresschecks the first and the last report of a new and of a resumed download.TestProgressReaderIntervalchecks the interval withtesting/synctest.TestLogProgresschecks the log lines.Deploy
Breaking change:
DownloadConfig.Loggeris removed. To log to your own logger, setcfg.OnProgress = plhttp.LogProgress(logger).amphion (
czds.go,tranco.go,commoncrawl.go) does not setLogger, 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%.