Repository navigation
feat: add resumable downloads, 429 retries and atomic file writes - #47
Merged
Merged
Conversation
Do retried network errors and 5xx responses, but not 429, and it ignored Retry-After. A request with Retries now also retries a 429. The wait is the longer of the backoff and the Retry-After of the response. A Retry-After over two minutes ends the retries, so the client never asks again before the server allows it. RetryAfter is exported for callers that send their own requests.
amphion and ganymede each carry a copy of the same resumable download. Download writes to a part file, resumes it with Range and If-Range, syncs and renames only a complete body, and returns at once when the target file exists. It keeps the rate limit, user agent and redirect cap of the client, asks for the identity encoding so resumed offsets stay valid, and logs its progress at a fixed interval.
amphion alone writes files through a part file and a rename in three places, and only one of them syncs before the rename. WriteAtomic and CreateAtomic write to path + ".part", sync it to disk and rename it, so a reader sees the old file or the complete new one. A failed write removes the part file and leaves the old file in place.
Every known caller creates, writes and closes the file in one function, which the callback of WriteAtomic covers. An exported Atomic type would need to be documented as unsafe for concurrent use, and removing it later would break dependents. The package now exports WriteAtomic only; a type can still be added when a caller needs a file that stays open across functions.
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
Moves three helpers from amphion's
pkg/sourcesinto go-commons. ganymede has a copy of the same download code, and amphion has three versions of the atomic write.Doretries 429 as well as network errors and 5xx whenRetries > 0. The wait is the longer of the backoff and theRetry-Afterof the response (seconds or HTTP date). ARetry-Afterover two minutes ends the retries, so the client never asks again before the server allows it.http.RetryAfter(h, now)(exported for callers that send their own requests)RangewithIf-Rangeand the stored ETag, 416 when the part is already complete, sync and rename only for a complete body. Returns at once when the target file exists. Keeps the client's rate limit, user agent and redirect cap; ignoresTimeoutandMaxBody. Asks forAccept-Encoding: identity, because a transparently decompressed body breaks resume offsets. Logs progress everyProgressInterval(default 10 s).(*http.Client).Download(ctx, url, path, cfg),http.DownloadConfig,http.DefaultDownloadConfig()file: write topath + ".part", sync, rename. A failed write removes the part file and leaves the old file.file.WriteAtomic(path, write); the only export, because every known caller, amphion's Parquet writer included, writes the file inside one functionThe new package is named
file, notfs, so it does not clash withio/fsin imports.How I checked it
just cipasses: gofumpt,go fix, vet, staticcheck, build and tests.go test ./...on its own.RetryAfter: seconds, HTTP date, past date, invalid values.retryDelay: backoff againstRetry-After, and the cap.Downloadagainsthttp.ServeContent: new download, existing file, resume, changed file on the server, part file already complete, 404, progress log.WriteAtomic: new file, replace, failed write keeps the old file, no leftover part file.Deploy
Nothing to deploy. One behavior change for dependents: a request with
Retries > 0now also retries a 429. A caller with its own 429 loop (amphion'ssources.getJSON) should drop it after it updates go-commons.Follow-ups after a merge:
Download,file.WriteAtomic(also for the Parquet chunks and manifests) andDoJSON, and drop its own copies.Downloadinpkg/seed.