Skip to content

feat: add resumable downloads, 429 retries and atomic file writes - #47

Merged
dennis-tra merged 4 commits into
mainfrom
feat/download-retries-atomic-write
Nov 12, 2026
Merged

dennis-tra merged 4 commits into
mainfrom
feat/download-retries-atomic-write

Conversation

@dennis-tra

@dennis-tra dennis-tra commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Moves three helpers from amphion's pkg/sources into go-commons. ganymede has a copy of the same download code, and amphion has three versions of the atomic write.

Change API
Do retries 429 as well as network errors and 5xx when Retries > 0. The wait is the longer of the backoff and the Retry-After of the response (seconds or HTTP date). A Retry-After over 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)
Resumable download: part file, Range with If-Range and 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; ignores Timeout and MaxBody. Asks for Accept-Encoding: identity, because a transparently decompressed body breaks resume offsets. Logs progress every ProgressInterval (default 10 s). (*http.Client).Download(ctx, url, path, cfg), http.DownloadConfig, http.DefaultDownloadConfig()
New package file: write to path + ".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 function

The new package is named file, not fs, so it does not clash with io/fs in imports.

How I checked it

  • just ci passes: gofumpt, go fix, vet, staticcheck, build and tests.
  • Each commit builds, vets and passes go test ./... on its own.
  • New tests:
    • RetryAfter: seconds, HTTP date, past date, invalid values.
    • retryDelay: backoff against Retry-After, and the cap.
    • 429 retries against a test server.
    • Download against http.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.
  • Not tested: a real interrupted download over the network. The resume path is covered only with a prepared part file.

Deploy

Nothing to deploy. One behavior change for dependents: a request with Retries > 0 now also retries a 429. A caller with its own 429 loop (amphion's sources.getJSON) should drop it after it updates go-commons.

Follow-ups after a merge:

  • amphion: use Download, file.WriteAtomic (also for the Parquet chunks and manifests) and DoJSON, and drop its own copies.
  • ganymede: use Download in pkg/seed.

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.
@dennis-tra
dennis-tra merged commit 280b47f into main Nov 12, 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